tibberous Posted June 24, 2010 Share Posted June 24, 2010 I have a query like: select * from users where (select `State` from `locations` where `locations`.`id`=`users`.`id`)!='PA' or (select `State` from `locations` where `locations`.`id`=`users`.`id`)!='NJ' or (select `State` from `locations` where `locations`.`id`=`users`.`id`)!='NY' or (select `State` from `locations` where `locations`.`id`=`users`.`id`)!='TX' ... ect This is obviously terrible - if only because it makes the query impossibly long to read. How can I get around this? Quote Link to comment https://forums.phpfreaks.com/topic/205746-how-to-keep-from-repeating-subquery-in-where-clause/ Share on other sites More sharing options...
Mchl Posted June 24, 2010 Share Posted June 24, 2010 SELECT u.* FROM users AS u INNER JOIN locations AS l ON l.id = u.id WHERE l.state NOT IN('PA','NJ','NY',...) Quote Link to comment https://forums.phpfreaks.com/topic/205746-how-to-keep-from-repeating-subquery-in-where-clause/#findComment-1076650 Share on other sites More sharing options...
tibberous Posted June 30, 2010 Author Share Posted June 30, 2010 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/205746-how-to-keep-from-repeating-subquery-in-where-clause/#findComment-1078986 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.