mihail Posted February 15, 2007 Share Posted February 15, 2007 Hi guys, I'm a beginner in php/mysql and I need some help. I have a problem with a query. Example: I have some cities and in every city there could be some stores. A table (a relational table): City_id store_id 1 1 1 2 2 1 3 1 3 2 3 3 My input is two city_ids ($city_id1 and city_id2). I have to select all unique store_ids that are in both $city_id1 and city_id2. For example if $city_id1 = 1 and $city_id2 = 2 a result should be 1. Another: if $city_id1 = 1 and $city_id2 = 3 a result should be 1 and 2. Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/ Share on other sites More sharing options...
worldworld Posted February 15, 2007 Share Posted February 15, 2007 I think your query must be : $sql = "select store_id from tbl where City_id=\"$cid\""; $sql_rs = mysql_query($sql); while($rw = mysql_fetch_array($sql_rs)) { echo $rw["store_id"]; } Try it... Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185336 Share on other sites More sharing options...
mihail Posted February 15, 2007 Author Share Posted February 15, 2007 No, that is not correct, but thanks for your help anyway. Maybe I was not clear enough: I have to select store_ids where city_id = $city_id1 (that is group 1 of store_ids), then I have to select store_ids where city_id = $city_id2 (that is group 2 of store_ids) and the store_ids I need as a result are those distinct store_ids that are in both grups. For example: Group 1 store_ids – 1, 2, 4, 5, 9, 10, 11 Group 2 store_ids –2, 3, 5, 7, 9 Result – 2, 5, 9 God, I will bite my hand off with this one. I know there is something I'm missing here. Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185357 Share on other sites More sharing options...
fenway Posted February 15, 2007 Share Posted February 15, 2007 Why not just issue two select queries, and union distinct them? Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185572 Share on other sites More sharing options...
mihail Posted February 15, 2007 Author Share Posted February 15, 2007 Hm, I don't want ALL distinct store_ids from both groups, I only want those distinct store_ids that are members of both grups. Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185709 Share on other sites More sharing options...
effigy Posted February 15, 2007 Share Posted February 15, 2007 Why doesn't this work? (select store_id from table where city_id = 1) union (select store_id from table where city_id = 2); Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185739 Share on other sites More sharing options...
mihail Posted February 15, 2007 Author Share Posted February 15, 2007 Why doesn't this work? (select store_id from table where city_id = 1) union (select store_id from table where city_id = 2); A result of this query is 1 and 2 but a result i'm looking for should be just 1. (select store_id from table where city_id = 1) gives 1 and 2 (select store_id from table where city_id = 2) gives 1 union distinct of these 2 queries gives 1 and 2 And again, I only want those distinct store_ids that are members of both grups, in that case it is only store_id 1. Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185758 Share on other sites More sharing options...
effigy Posted February 15, 2007 Share Posted February 15, 2007 I think this is what you want, but I'm wondering if there's a better way... SELECT store_id FROM (SELECT store_id, GROUP_CONCAT(city_id SEPARATOR ',') AS city_ids FROM table GROUP BY store_id) AS store_city_set WHERE FIND_IN_SET(1, city_ids) AND FIND_IN_SET(2, city_ids); Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185774 Share on other sites More sharing options...
shoz Posted February 15, 2007 Share Posted February 15, 2007 I think this is what you want, but I'm wondering if there's a better way... This should be better assuming it gives the result the OP is looking for. SELECT store_id, COUNT(*) AS cnt FROM tablename WHERE city_id IN($city_id1, $city_id2) GROUP BY store_id HAVING cnt = 2 A UNIQUE multi-column index should logically be on (cityid, storeid) and is required to ensure the query's future accuracy. EDIT: You should compare cnt to the number of $city_id's in the WHERE clause Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185777 Share on other sites More sharing options...
effigy Posted February 15, 2007 Share Posted February 15, 2007 shoz: Beautiful. Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185785 Share on other sites More sharing options...
mihail Posted February 15, 2007 Author Share Posted February 15, 2007 Aaaauch! That is mind blowing, guys!!! But it works! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185787 Share on other sites More sharing options...
fenway Posted February 15, 2007 Share Posted February 15, 2007 shoz: Beautiful. Sorry, I misread the question... but yes, very elegant; I've seen similar problems solved with self joins, too. Quote Link to comment https://forums.phpfreaks.com/topic/38605-solved-self-join/#findComment-185816 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.