2016-04-13 83 views
-1
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.If當前user_id1加入心願,母雞我必須可以從另一表中的值使用查詢發送願望清單hotel_id's作爲列名wishlisthotels表中。如何使用子查詢

我試過這樣

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" 

但它告訴我像subquery return more than one value

我應該怎麼做,有人幫助我..

回答

0

你可以直接使用JOIN實現這一錯誤

SELECT h。* from abserve_hotels LEFT JOIN wishlists w on h.id = w.hotel_id WHERE w.user_id = 1 GROUP by w.hotel_id