spires Posted December 16, 2009 Share Posted December 16, 2009 Hi, I want to select all from my database where the months column is set to '24 Month' and also set to ''. SELECT * FROM csv_item WHERE item_contract_length='' && item_contract_length='24 Month' However, every time I try it, it just brings back no results, even though both of them work on there own. Any ideas please, as I really stuck. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/185361-simple-sql-not-working/ Share on other sites More sharing options...
Mchl Posted December 16, 2009 Share Posted December 16, 2009 Can anything be '24 month' long and empty at the same time? Use OR instead of AND Quote Link to comment https://forums.phpfreaks.com/topic/185361-simple-sql-not-working/#findComment-978519 Share on other sites More sharing options...
spires Posted December 16, 2009 Author Share Posted December 16, 2009 Thanks for your help OR will not work. What i'm trying to do is; In the database there are 5 options in the column - '', '6 Month', '12 Month', '24 month', '36 Month' I have a dropdown list that has all of the options apart from the blank one. If the field is empty, I want it to select the selected option i.e 24 Month AND all rows that have a black field Other wise, all rows that are black will never show up in the list. Here is the full code: SELECT * FROM csv_item WHERE item_group='$iSearch' && item_network='$netSearch' && item_contract_length='' && item_contract_length='$clSearch' ORDER by item_internal_name AS Any more ideas would be a great help. Quote Link to comment https://forums.phpfreaks.com/topic/185361-simple-sql-not-working/#findComment-978533 Share on other sites More sharing options...
Mchl Posted December 16, 2009 Share Posted December 16, 2009 I guess you know better. Did you try at least? http://en.wikipedia.org/wiki/Boolean_logic SELECT * FROM csv_item WHERE item_group='$iSearch' AND item_network='$netSearch' AND (item_contract_length='' OR item_contract_length='$clSearch' OR item_contract_length IS NULL) ORDER by item_internal_name AS Quote Link to comment https://forums.phpfreaks.com/topic/185361-simple-sql-not-working/#findComment-978542 Share on other sites More sharing options...
spires Posted December 16, 2009 Author Share Posted December 16, 2009 Yes, I just tried. It either gives me '24 Month' OR 'blank' I need to be able to select both at once: '24 Month' AND 'blank' Any more ideas? Quote Link to comment https://forums.phpfreaks.com/topic/185361-simple-sql-not-working/#findComment-978544 Share on other sites More sharing options...
spires Posted December 16, 2009 Author Share Posted December 16, 2009 Thanks for your help guys. Thia is the final block of code. It was item_contract_length IN('','$clSearch') that I was after. $item_sql = "SELECT * FROM csv_item WHERE item_group='$iSearch' && item_network='$netSearch' && item_contract_length IN('','$clSearch') ORDER by item_internal_name ASC"; I needed to select both black AND month at the same time. Quote Link to comment https://forums.phpfreaks.com/topic/185361-simple-sql-not-working/#findComment-978571 Share on other sites More sharing options...
Mchl Posted December 16, 2009 Share Posted December 16, 2009 Did you try the query I posted? It would work just as well. You can't have a row that is blank AND '24 month' long at one time. You need to select all rows that are '24 month' long OR blank You can consider IN() as alternate syntax for OR (this simplifies things a little, but for this example that's irrelevant) Quote Link to comment https://forums.phpfreaks.com/topic/185361-simple-sql-not-working/#findComment-978593 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.