WatsonN Posted October 12, 2010 Share Posted October 12, 2010 Is there a way to have multiple WHERE statements in a mysql . Like: SELECT * FROM YBK_Ads WHERE UID = '{$_GET['UID']}' and WHERE delete = '0' That doesn't work but is it possible Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/ Share on other sites More sharing options...
Alex Posted October 12, 2010 Share Posted October 12, 2010 You don't need the second "WHERE". SELECT * FROM YBK_Ads WHERE UID = '{$_GET['UID']}' and delete = '0' Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121562 Share on other sites More sharing options...
WatsonN Posted October 12, 2010 Author Share Posted October 12, 2010 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 'delete = '0'' at line 1 I used what you said and I got that Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121563 Share on other sites More sharing options...
roopurt18 Posted October 12, 2010 Share Posted October 12, 2010 DELETE is a reserved word. Surround it with backticks: ` SELECT * FROM `YBK_Ads` WHERE `UID` = '{$_GET['UID']}' and `delete` = '0' Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121566 Share on other sites More sharing options...
WatsonN Posted October 12, 2010 Author Share Posted October 12, 2010 Works perfectly Thank Ya much Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121569 Share on other sites More sharing options...
WatsonN Posted October 12, 2010 Author Share Posted October 12, 2010 One more quick question. Could I put the WHERE clause in this, or would it break it? SELECT y.*,COUNT(ads.UID) AS posts FROM YBK_Login AS y LEFT JOIN YBK_Ads AS ads ON ads.UID=y.ID GROUP BY y.ID Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121573 Share on other sites More sharing options...
roopurt18 Posted October 12, 2010 Share Posted October 12, 2010 Yes. SELECT y.*, COUNT(ads.UID) AS posts FROM YBK_Login AS y LEFT JOIN YBK_Ads AS ads ON ads.UID=y.ID WHERE ... GROUP BY y.ID Notice how the use of white space makes the query logic easier to follow? Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121578 Share on other sites More sharing options...
WatsonN Posted October 12, 2010 Author Share Posted October 12, 2010 It sure does. I used SELECT y.*, COUNT(ads.UID) AS posts FROM YBK_Login AS y LEFT JOIN YBK_Ads AS ads ON ads.UID=y.ID WHERE `delete` = '0' GROUP BY y.ID And any field that was 0 was not displayed Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121582 Share on other sites More sharing options...
jcbones Posted October 12, 2010 Share Posted October 12, 2010 You will have to add the table name to any column when using a join query. SELECT y.*, COUNT(ads.UID) AS posts FROM YBK_Login AS y LEFT JOIN YBK_Ads AS ads ON ads.UID=y.ID WHERE ads.`delete` = '0' GROUP BY y.ID Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121583 Share on other sites More sharing options...
WatsonN Posted October 12, 2010 Author Share Posted October 12, 2010 That still pulls all rows with 0 as their value out. Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121584 Share on other sites More sharing options...
roopurt18 Posted October 12, 2010 Share Posted October 12, 2010 And any field that was 0 was not displayed That still pulls all rows with 0 as their value out. So which is it? Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121597 Share on other sites More sharing options...
WatsonN Posted October 12, 2010 Author Share Posted October 12, 2010 Any field that is 0 Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121613 Share on other sites More sharing options...
Zane Posted October 12, 2010 Share Posted October 12, 2010 Perhaps take 0 out of quotes? Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121614 Share on other sites More sharing options...
WatsonN Posted October 12, 2010 Author Share Posted October 12, 2010 Nope, even out of quotes it still does the same thing Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121616 Share on other sites More sharing options...
Zane Posted October 12, 2010 Share Posted October 12, 2010 Try an INNER JOIN instead... and if the delete column is varchar, put the quotes back. I should have asked that to begin with. Alternatively, you could try an OUTER JOIN, but I would try INNER first. Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121621 Share on other sites More sharing options...
WatsonN Posted October 13, 2010 Author Share Posted October 13, 2010 I fixed it by creating a new user and posting all deleted posts to that user so they don't show up under other people. I will keep looking at this and if I find something I will post back. But, for now SOLVED! Because the original problem was solved. Thank yall so much for the help Quote Link to comment https://forums.phpfreaks.com/topic/215727-multiple-where-clauses/#findComment-1121961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.