iceblox Posted May 18, 2009 Share Posted May 18, 2009 Hi All, Im trying to display data from my table called "error" but only if the text in the "model" column is not in the "deals_models" table in the "ModelID" column. This is my query so far. $query = "SELECT * FROM error WHERE affiliate = '$aff' AND model NOT IN (SELECT ModelID FROM deals_models) AND model <> '0' GROUP BY model ORDER BY model DESC"; But it only seems to be displaying some of these correct for example. I have "E63 Blue" in the error table but this is not a ModelID in that table and it isnt displaying. However it will display "8800" which isnt in the deals_models table. I think it might be something to do with the structure of the tables. ModelID is set up a "INT" and "model" is set up as "VARCHAR" Could this be the reason why it is not working? Any suggestions would be greatly appreciated. Thanks, Phil Quote Link to comment https://forums.phpfreaks.com/topic/158617-solved-not-in-query/ Share on other sites More sharing options...
RussellReal Posted May 18, 2009 Share Posted May 18, 2009 SELECT * FROM error JOIN deals_models ON (error.model != deals_models.ModelID) WHERE `affiliate` = '$aff' Quote Link to comment https://forums.phpfreaks.com/topic/158617-solved-not-in-query/#findComment-836557 Share on other sites More sharing options...
iceblox Posted May 18, 2009 Author Share Posted May 18, 2009 Hi Russell, Thanks for your reply. I tried the code you posted. $query = "SELECT * FROM error JOIN deals_models ON (error.model != deals_models.ModelID) WHERE `affiliate` = '$aff' GROUP BY model"; This displays the ones that it didnt before i.e "E63 Blue" but it also displays the ones that are in the models table. Im trying to set it not to display the ones in the deals_models table. Any other suggestions? Thanks for your help Phil Quote Link to comment https://forums.phpfreaks.com/topic/158617-solved-not-in-query/#findComment-836560 Share on other sites More sharing options...
RussellReal Posted May 18, 2009 Share Posted May 18, 2009 is error.model an id number? Quote Link to comment https://forums.phpfreaks.com/topic/158617-solved-not-in-query/#findComment-836561 Share on other sites More sharing options...
kickstart Posted May 18, 2009 Share Posted May 18, 2009 Hi You might well be right with the thought that it doesn't appreciate different data types. If so try this:- $query = "SELECT * FROM error WHERE affiliate = '$aff' AND model NOT IN (SELECT CONVERT(ModelID, CHAR) FROM deals_models) AND model <> '0' GROUP BY model ORDER BY model DESC"; You could also try something like:- $query = "SELECT * FROM error a LEFT OUTER JOIN deals_models b ON a.model = CONVERT(b.ModelID,CHAR) WHERE affiliate = '$aff' AND model <> '0' AND b.ModelID IS NULL GROUP BY model ORDER BY model DESC"; All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/158617-solved-not-in-query/#findComment-836564 Share on other sites More sharing options...
iceblox Posted May 18, 2009 Author Share Posted May 18, 2009 Thanks for all your help guys! model isn't an id Russell just a text string, i think this is where it was going wrong. I tried your first example you gave me Keith and it worked a treat! Just how i wanted it! Thanks for you help guys it is much appreciated! Thanks again, Phil Quote Link to comment https://forums.phpfreaks.com/topic/158617-solved-not-in-query/#findComment-836572 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.