Click here to Skip to main content
15,888,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a select statement which causes a timeout in c# coding and is also not answered using phpMyAdmin:
SQL
SELECT * FROM 'tzinterst' WHERE 'tzinterst.ZINTERNR' IN
(SELECT 'tzinterl.ZINTERNR' FROM 'tzinterl' WHERE 'tzinterl.YHNUMMER' IN
 (SELECT 'tzstoffl.YHNUMMER' FROM 'tzstoffl' WHERE 'tzstoffl.ZNUMM' IN
  (SELECT 'tzspez.ZNUMM' FROM 'tzspez' WHERE 'ZLNUMM' = '008096'))) order by 'tzinterst.ZART';


In execution I receive an error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''tzinterst' WHERE 'tzinterst.ZINTERNR' IN (SELECT 'tzinterl.ZINTERNR' FROM 'tzi' at line 1

The table sizes vary between 10.000 and 20.000 rows.
I checked and rechecked all the spelling and existence of items in the tables, there is no error, what I am doing wrong?
Please help.
Your's MiKr41

Sorry, omitting the marks changes only the type of error: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The coding looks like this:
SQL
MySqlDataAdapter DBV = new MySqlDataAdapter("SELECT * FROM tzinterst WHERE tzinterst.ZINTERNR IN " +
"(SELECT tzinterl.ZINTERNR FROM tzinterl WHERE tzinterl.YHNUMMER IN " +
 "(SELECT tzstoffl.YHNUMMER FROM tzstoffl WHERE tzstoffl.ZNUMM IN " +
  "(SELECT tzspez.ZNUMM FROM tzspez WHERE ZLNUMM = '" + txt_ZLNUMM.Text + "'))) order by tzinterst.ZART", DBConn);


My workaround works fine:
C#
MySqlDataAdapter DBspez = new MySqlDataAdapter("select ZNUMM from tzspez where ZLNUMM = '" + txt_ZLNUMM.Text + "'", DBConn);
DataSet sSp = new DataSet();
if (DBspez.Fill(sSp) > 0)
{
    sIA = new string[500];
    foreach (DataRow dr in sSp.Tables[0].Rows)
    {
        MySqlDataAdapter sst = new MySqlDataAdapter("select YHNUMMER from tzstoffl where ZNUMM = '" + dr["ZNUMM"].ToString() + "'", DBConn);
        DataSet st = new DataSet();
        if (sst.Fill(st) > 0)
        {
            foreach (DataRow dt in st.Tables[0].Rows)
            {
                MySqlDataAdapter sTd = new MySqlDataAdapter("select ZINTERNR from tzinterl where YHNUMMER = '" + dt["YHNUMMER"].ToString() + "'", DBConn);
                DataSet sT = new DataSet();
                if (sTd.Fill(sT) > 0)
                {
                    foreach (DataRow sTr in sT.Tables[0].Rows)
                    {
                        MySqlDataAdapter sIt = new MySqlDataAdapter("select * from tzinterst where ZINTERNR = '" + sTr["ZINTERNR"] + "' order by ZART desc", DBConn);
                        DataSet sIts = new DataSet();
                        if (sIt.Fill(sIts) > 0)
                        {
                            bool takeIt;
                            int i = 0;
                            foreach (DataRow sItr in sIts.Tables[0].Rows)
                            {
                                takeIt = true;
                                for (int j = 1; j < sIts.Tables[0].Rows.Count; j++)
                                {
                                    if (sIA[j] == sItr["ZINTERGR1"].ToString()) takeIt = false;
                                }
                                if (takeIt)
                                {
                                    sIA[i] = sItr["ZINTERGR1"].ToString();
                                    string[] zin = sItr["ZTEXT"].ToString().Split((char)166);
                                    for (int k = 0; k < zin.Length; k++) rtf_Text.Text += zin[k] + "\r\n";
                                    i++;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
else MessageBox.Show("Keine Interaktionstexte gefunden!");
;

But it is not at all an elegant solution - right?
Your's MiKr41
Posted
Updated 7-Nov-13 3:58am
v2

1 solution

Try this
SQL
SELECT * FROM tzinterst
WHERE tzinterst.ZINTERNR IN
(
    SELECT tzinterl.ZINTERNR FROM tzinterl
    WHERE tzinterl.YHNUMMER IN
    (
        SELECT tzstoffl.YHNUMMER FROM tzstoffl
        WHERE tzstoffl.ZNUMM IN
        (
            SELECT tzspez.ZNUMM FROM tzspez WHERE ZLNUMM = '008096'
        )
    )
) order by tzinterst.ZART;


[EDIT in response to OP comments]
All I did above was correct your syntax error. To make it more performant and read better try the following (caveat - I haven't been able to check this)
SQL
SELECT INTERST.*
FROM tzspez SPEZ
LEFT OUTER JOIN tzstoffl STOFF1 ON SPEZ.ZNUMM = STOFF1.ZNUMM
LEFT OUTER JOIN tzinterl INTER1 ON STOFF.YHNUMMER = INTER1.YHNUMMER
LEFT OUTER JOIN tzinterst INTERST ON INTER1.ZINTERNR = INTERST.ZINTERNR
WHERE SPEZ.ZLNUMM = '008096' ORDER BY INTERST.ZART


Couple of other things to note ... you really should not do things like
= '" + txt_ZLNUMM.Text + "'"
as this leaves you vulnerable to SQL Injection attacks (and also makes it harder to debug complex sql statements).

Use Parameters instead - here's an article to get you started from dotnetperls[^]

You may need to tune up your database by using Indexes on ZNUMM, YHNUMMER etc - useful free book - Use the index Luke[^]
 
Share this answer
 
v2
Comments
Member 10284036 27-Nov-13 6:23am    
Trying this coding above there is no answer and no message (is there a loop entered?) e.g.
trying
select ZNUMM from tzspez where ZLNUMM = '008096';
works perfect and results 26936;
then
select YHNUMMER FROM tzstoffl
WHERE ZNUMM = '26936;' works perfect too and results an array of 5 strings.
doing
select YHNUMMER FROM tzstoffl
WHERE ZNUMM in (select ZNUMM from tzspez where ZLNUMM = '008096');
gives no result (now I am waiting already 10 minutes.
What is wrong? Please help MiKr41
CHill60 27-Nov-13 12:05pm    
I've updated my solution above.
Maciej Los 27-Nov-13 13:03pm    
Good job!
+5!
CHill60 27-Nov-13 16:27pm    
Thank you!
Member 10284036 29-Nov-13 5:33am    
Thank you very much CHill60 - it work's now! Your's MiKr41

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900