如何使用distinct在mysql中使用join两个表

时间:2017-06-07 08:40:57

标签: mysql distinct

这是我的sql查询,显示价格低于500的酒店

select a.*, b.room_price, b.hotel_id  
from hotels a , hotel_room_type b 
where a.sub_destination_id=1 
and a.destination_id=1 and 
a.id = b.hotel_id 
and b.room_price BETWEEN 0 AND 500

这里显示重复记录 ids 190来了两次,我该怎么解决

2 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您只需要删除冗余数据。您可以使用group by子句来实现此目的。

SQL> SELECT a.*, b.room_price, b.hotel_id FROM hotels a INNER JOIN  hotel_room_type b ON a.id =b.hotel_id where a.sub_destination_id=1 AND a.destination_id=1 AND b.room_price BETWEEN 0 AND 500 GROUP BY a.id

答案 1 :(得分:0)

试试这个

SELECT a.*, 
   b.room_price, 
   b.hotel_id 
FROM   hotels a 
   INNER JOIN hotel_room_type b 
           ON a.id = b.hotel_id 
WHERE  a.sub_destination_id = 1 
   AND a.destination_id = 1 
   AND b.room_price BETWEEN 0 AND 500 
GROUP  BY a.id