sql - MySQL Inner join with where on multiple rows -


i need sql solution following:

lets have 1 table named "trips"

[  id ][ name  ][ duration] [  1  ][ trip1 ][   12    ] [  2  ][ trip2 ][   16    ] [  3  ][ trip3 ][   5     ]  

and table countries each trip visits: locations

[  tripid ][ country ] [    1    ][      ] <-- [    1    ][    pa   ] <-- [    1    ][    ru   ] [    2    ][      ] <-- [    2    ][    pa   ] <-- [    3    ][    pa   ] [    3    ][    ru   ] 

now want trips visitis "us" , "pa". result 1 , 2 becouse both visit , pa.

i thought use inner join takes 1 row in locations table (afaik)

can me?

[edit]

i have working ugly way:

select * ,group_concat(distinct(locations.country) separator ',') countrycodes  trips left join locations on trips.id = locations.tripid   group trips.id  having countrycodes '%pa%' , countrycodes '%us%'; 

but think using "like" ugly solution

select *, count(distinct tripid) cnt trips left join locations on trips.id = locations.tripid locations.country in ('us', 'pa') group trips.id having cnt = 2 

basically, records trip pa or us, count how many trips made, , return both countries visited.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -