abserve_hotels
hotel_id name
1 A
2 B
3 C
4 D
4 E
6 G
7 F
wishlists
id user_id hotel_id
1 5 1
2 5 2
3 7 1
4 1 7
5 1 5
我有这两个表,我需要检查当前用户将哪些酒店添加到心愿单。如果当前user_id
是1
,那么我必须发送心愿单{{ 1}}使用查询作为hotel_id's
表中的列名wishlist
。
我试过这个
hotels
但是它显示了像SELECT `h`.*,(SELECT `w`.`hotel_id` FROM `wishlists` AS `w` JOIN `abserve_hotels` AS `ah` ON `w`.`hotel_id` = `ah`.`hotel_id` AND `w`.`user_id` = 1 GROUP BY `w`.`hotel_id`)as `wish` FROM `abserve_hotels` AS `h` WHERE 1 AND `city` = "madurai" AND `country` = "india"
我该怎么做,有人帮助我......
答案 0 :(得分:0)
您可以使用JOIN直接实现此目的
选择h。*来自abserve_hotels LEFT JOIN愿望清单w on h.id = w.hotel_id WHERE w.user_id = 1 GROUP by w.hotel_id