如何在mysql中使用distinct连接两个表

如何在mysql中使用distinct连接两个表,mysql,distinct,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已经来过两次了,我如何解决 如果我正确理解您

这是我的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已经来过两次了,我如何解决

如果我正确理解您的问题,您只需删除冗余数据即可。您可以通过使用子句来实现这一点

试试这个

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 

使用
SELECT DISTINCT
,就像您在标题中所说的那样。您应该将图像保存为png以保存质量使用此选择DISTINCT a.*,b.room\u price,b.hotel\u id from hotels a,hotel_room_类型b,其中a.sub_destination_id=1和a.destination_id=1和a.id=b。hotel_id和b.room_价格介于0和500之间,每个房间可获得一行。如果一家酒店有不止一种类型的房间(通常情况下),您将得到重复的行。这正是你对数据库的要求。
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