jyushinx Posted October 8, 2008 Share Posted October 8, 2008 So this is my issue: I have a table that houses a large number of Items. There is another table that houses award information for certain items. This table is essentially comprised of ItemId and AwardId. I need a single query statement that will mark which items have awards when a generic list of items is requested. The purpose would be to include a special graphic next to the items that have awards. This is basically a statement that will let me know if each element exists in another table. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/127502-solved-identifying-select-elements-that-exist-in-another-table/ Share on other sites More sharing options...
fenway Posted October 11, 2008 Share Posted October 11, 2008 You can use a LEFT JOIN or an EXISTS query. Quote Link to comment https://forums.phpfreaks.com/topic/127502-solved-identifying-select-elements-that-exist-in-another-table/#findComment-662871 Share on other sites More sharing options...
jyushinx Posted October 13, 2008 Author Share Posted October 13, 2008 I figured it would be with a LEFT JOIN, but when I was trying it out, I couldn't get it to work correctly. Let's do an example... Say we are doing a search for elements in the Items table that are of CategoryType 2. Now of these returned elements, we need to know which ones have won awards. So the basic SQL (without the LEFT JOIN) would be: SELECT ItemName,ItemPrice,ItemDescription FROM ItemsTable WHERE CategoryType=2 What would the SQL be so we ended up with another column that designates whether or not the item won an award? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/127502-solved-identifying-select-elements-that-exist-in-another-table/#findComment-664409 Share on other sites More sharing options...
Barand Posted October 13, 2008 Share Posted October 13, 2008 SELECT i.ItemName, i.ItemPrice, i.ItemDescription, a.awardid FROM ItemsTable i LEFT JOIN awards_table a ON i.id = a.itemid WHERE CategoryType=2 Where no award, awardid col will be NULL Quote Link to comment https://forums.phpfreaks.com/topic/127502-solved-identifying-select-elements-that-exist-in-another-table/#findComment-664493 Share on other sites More sharing options...
jyushinx Posted November 16, 2008 Author Share Posted November 16, 2008 Sorry for the extreme delay. I've been away. I wanted to post that Barand's solution plus an additional tweak solved the issue. Because certain items can win multiple awards, and I wanted only distinct items returned, I added a GROUP BY. So: SELECT lj.* FROM (SELECT i.id, i.ItemName, i.ItemPrice, i.ItemDescription, a.awardid FROM ItemsTable i LEFT JOIN awards_table a ON i.id = a.itemid WHERE CategoryType=2) AS lj GROUP BY lj.id Quote Link to comment https://forums.phpfreaks.com/topic/127502-solved-identifying-select-elements-that-exist-in-another-table/#findComment-691542 Share on other sites More sharing options...
jyushinx Posted November 16, 2008 Author Share Posted November 16, 2008 Quick change. I posted the code before I refined it a bit more: SELECT i.id, i.ItemName, i.ItemPrice, i.ItemDescription, a.awardid FROM ItemsTable i LEFT JOIN awards_table a ON i.id = a.itemid WHERE CategoryType=2 GROUP BY i.id Quote Link to comment https://forums.phpfreaks.com/topic/127502-solved-identifying-select-elements-that-exist-in-another-table/#findComment-691552 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.