programistas Posted March 23, 2011 Share Posted March 23, 2011 hey, thanks in advance if someone can help me x y auto 1 2 a 5 3 b 4 6 c 1 2 d 7 7 e 4 6 f 5 3 b 9 4 h 4 6 f I need query to get from table auto_history(x, y, auto) only these cars, that are in the same place with other cars The result must be : a - because (it has x=1 and y=2 AND d has also x=1 and y=2), so they are in the same place d - because (it has x=1 and y=2 and a has also x=1 and y=2), so they are in the same place .... so, that's what my sql result must give a, c, d, f Link to comment https://forums.phpfreaks.com/topic/231456-crazy-query-help/ Share on other sites More sharing options...
jcbones Posted March 23, 2011 Share Posted March 23, 2011 $table = 'table_name'; $query = "SELECT a.auto FROM $table AS a JOIN $table AS b ON (a.x = b.x AND a.y = b.y AND a.auto != b.auto) ORDER BY a.auto"; Link to comment https://forums.phpfreaks.com/topic/231456-crazy-query-help/#findComment-1191137 Share on other sites More sharing options...
programistas Posted March 23, 2011 Author Share Posted March 23, 2011 Yes, that's cool, I assumed, that it's somehow related to joins, but , from the theory what I read on tutorials, I remember that "we use joins when we need to work on more than 1 table "., so thank you. Link to comment https://forums.phpfreaks.com/topic/231456-crazy-query-help/#findComment-1191141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.