ultrus Posted June 23, 2010 Share Posted June 23, 2010 Maybe I'm just having a bad night, so please slap me in the face if needed. I have a table like this: Table: system_contents +-----+--------+--------------+ | id | name | contains | +-----+--------+--------------+ | 1 | Sys1 | asteroids | | 2 | Sys2 | asteroids | | 3 | Sys2 | planets | +-----+--------+--------------+ I want to select systems that contain asteroids AND planets. The following query will NOT work for that because it selects systems with asteroids OR planets (right?): SELECT name FROM system_contents WHERE contains IN('asteroids','planets') GROUP BY name Any thoughts on how to adjust? Thanks much in advance! Quote Link to comment https://forums.phpfreaks.com/topic/205604-i-need-something-like-in123-but-more-like-in1-and-2-and-3/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 23, 2010 Share Posted June 23, 2010 Assuming you don't have duplicates (i.e. sys2, asteroids, sys2, asteroids), this should (untested) work - SELECT name, count(*) as cnt FROM system_contents WHERE contains IN('asteroids','planets') GROUP BY name HAVING cnt = 2 Quote Link to comment https://forums.phpfreaks.com/topic/205604-i-need-something-like-in123-but-more-like-in1-and-2-and-3/#findComment-1075893 Share on other sites More sharing options...
ultrus Posted June 23, 2010 Author Share Posted June 23, 2010 PFMaBiSmAd, Nice! I didn't know about 'HAVING'. However... that table I showed is a bit simplified. Sometimes there are multiple planets with different planet names in the same system (honestly, asteroid doesn't fit, it's actually different planet types) I like the count addition. If getting it to work properly, it would be nice to know how many total matched entities there are per system. I'll do some more research, but any additional feedback again is much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/205604-i-need-something-like-in123-but-more-like-in1-and-2-and-3/#findComment-1075909 Share on other sites More sharing options...
ultrus Posted June 23, 2010 Author Share Posted June 23, 2010 Does something like this make more sense? I could probably generate something like this dynamically: SELECT c1.name, count(*) as cnt FROM system_contents c1 LEFT JOIN system_contents c2 ON c1.id = c2.id WHERE c1.contains = 'asteroids' AND c2.contains = 'planets' GROUP BY name Now that I type that, it doesn't make sense. Perhaps some sleep will help. EDIT: Another rendition: SELECT c1.name, count(*) as cnt FROM system_contents c1 LEFT JOIN system_contents c2 ON c1.name = c2.name WHERE c1.contains = 'asteroids' AND c2.contains = 'planets' AND c1.contains != c2.contains GROUP BY name Quote Link to comment https://forums.phpfreaks.com/topic/205604-i-need-something-like-in123-but-more-like-in1-and-2-and-3/#findComment-1075914 Share on other sites More sharing options...
bluejay002 Posted June 23, 2010 Share Posted June 23, 2010 Am not sure if I am getting what you want but you may try this if you want: SELECT name FROM system_contents WHERE contains LIKE '%asteroids%' AND contains LIKE '%planets%' GROUP BY name Though am not really getting if this is what you want to achieve though. bluejay, Quote Link to comment https://forums.phpfreaks.com/topic/205604-i-need-something-like-in123-but-more-like-in1-and-2-and-3/#findComment-1075962 Share on other sites More sharing options...
fenway Posted June 24, 2010 Share Posted June 24, 2010 ultrus has the right idea. Quote Link to comment https://forums.phpfreaks.com/topic/205604-i-need-something-like-in123-but-more-like-in1-and-2-and-3/#findComment-1076381 Share on other sites More sharing options...
ultrus Posted June 24, 2010 Author Share Posted June 24, 2010 I'm still digging, and will post a solution as early as this morning. Quote Link to comment https://forums.phpfreaks.com/topic/205604-i-need-something-like-in123-but-more-like-in1-and-2-and-3/#findComment-1076593 Share on other sites More sharing options...
ultrus Posted June 24, 2010 Author Share Posted June 24, 2010 OK. I got it figured out. Instead of going crazy, joining a non-normalized table with itself many, many times, I just created a new table containing all the information I will need. The final query that creates the table in its full context is now available to the general public here: http://wiki.eve-id.net/Create_Table_with_Planet_Type_Counts_in_Each_System Quote Link to comment https://forums.phpfreaks.com/topic/205604-i-need-something-like-in123-but-more-like-in1-and-2-and-3/#findComment-1076689 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.