database - MySQL mutual condition query with single table -


i receiving user_id 1 , 2 post. want hospital id sharing both user 4.

user_hopitals table

 id | user_id | hospital_id  1  |   1     |    4  2  |   2     |    4  3  |   1     |    5  4  |   2     |    9 

hospitals table

id | name  4  | abc hospital 5  | xyz hospital 9  | def hospital 

i want data

 hospital_id | hospital_name      4       | abc hospital 

you can use self join, e.g.:

select h.id. h.name user_hospitals uh1 join user_hospitals uh2 on uh1.hospital_id = uh2.hospital_id join hospitals h on uh1.hospital_id = h.id uh1.user_id = 1 , uh2.user_id = 2; 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -