php_fish Posted October 17, 2009 Share Posted October 17, 2009 I have 3 tables; #######table content#### idcontent title copy ------------------------------------ 1 title1 text1 2 title2 text2 3 title3 text3 4 title4 text4 #######table images #### idimg file ------------------- 1 img1.jpg 2 img2.jpg 3 img3.jpg 4 img4.jpg ####### assigned images #### idassign idcontent idimg ------------------------------------------ 1 1 2 2 1 1 3 3 2 4 4 3 I'm trying to build a query that will give me all images that are not assigned to a specific content id. Because the same image can be assigned to any number of contentid's I'm finding it tricky. The sql I'm using at the moment is a simple select all images SELECT file, idimg FROM images I've tried various other queries involving joins and sub queries but I just can't work it out. MySQL is not my strongest skill Any help will be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/178062-solved-omitting-specifuc-data-in-resuls-table/ Share on other sites More sharing options...
fenway Posted October 18, 2009 Share Posted October 18, 2009 Why not a LEFT JOIN... IS NULL? NOT EXISTS might work too. Quote Link to comment https://forums.phpfreaks.com/topic/178062-solved-omitting-specifuc-data-in-resuls-table/#findComment-938920 Share on other sites More sharing options...
php_fish Posted October 19, 2009 Author Share Posted October 19, 2009 I'm not getting very far, have spent several days trying out your suggestion but with no luck. Can anyone advice me on the syntax of such a query. Can it be done without the intervention of php? Quote Link to comment https://forums.phpfreaks.com/topic/178062-solved-omitting-specifuc-data-in-resuls-table/#findComment-939569 Share on other sites More sharing options...
kickstart Posted October 19, 2009 Share Posted October 19, 2009 Hi Can be done in the way Fenway suggested. Something like this:- SELECT a.idimg FROM images a LEFT OUTER JOIN assignedimages b ON a.idimg = b.idimg WHERE b.idimg IS NULL Basically join the table of images using a LEFT OUTER JOIN to the link table. This should give you one or more returned rows for each image, one for each entry on the link table. If there is nothing on the link table then it will return a row with NULL fields from the link table. The query merely discards all except those which have NULL fields on the link table. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/178062-solved-omitting-specifuc-data-in-resuls-table/#findComment-939574 Share on other sites More sharing options...
php_fish Posted October 19, 2009 Author Share Posted October 19, 2009 tried that, it doesn't give me want I want. I want all images except for those assigned to record 1 in he content table (for example). Since an image can be assigned to more than one content record that query still returns the images that record 1 has assigned to it because other content records have the same images assigned. Quote Link to comment https://forums.phpfreaks.com/topic/178062-solved-omitting-specifuc-data-in-resuls-table/#findComment-939597 Share on other sites More sharing options...
fenway Posted October 19, 2009 Share Posted October 19, 2009 Well, then you need to specify this content id in your ON clause. Quote Link to comment https://forums.phpfreaks.com/topic/178062-solved-omitting-specifuc-data-in-resuls-table/#findComment-939636 Share on other sites More sharing options...
php_fish Posted October 19, 2009 Author Share Posted October 19, 2009 I've tried; SELECT a.idimg FROM images a LEFT OUTER JOIN assignedimages b ON a.idimg = b.idimg WHERE b.idimg IS NULL AND assignedimages.idcontent != 1 again with no luck Quote Link to comment https://forums.phpfreaks.com/topic/178062-solved-omitting-specifuc-data-in-resuls-table/#findComment-939678 Share on other sites More sharing options...
kickstart Posted October 21, 2009 Share Posted October 21, 2009 Hi The way you have tried it will throw them away after the join. You want to discard them before the join:- SELECT a.idimg FROM images a LEFT OUTER JOIN assignedimages b ON a.idimg = b.idimg AND b.idcontent = 1 WHERE b.idimg IS NULL All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/178062-solved-omitting-specifuc-data-in-resuls-table/#findComment-940984 Share on other sites More sharing options...
php_fish Posted October 21, 2009 Author Share Posted October 21, 2009 Yes, that works perfectly. Once again you've helped me out. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/178062-solved-omitting-specifuc-data-in-resuls-table/#findComment-941373 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.