使用内部联接时重复 MYSQL 个结果
Duplicate on MYSQL results when using inner join
我正在尝试列出两个属性(booking.roomno 和 room.price),条件是来自两个不同表的 "booking.DATETO is not null"。
Table:预订! Table:Room!
我试过使用这个命令
select booking.roomno,room.price
from booking
inner join room on booking.ROOMNO=room.roomno
where booking.dateto is not null
尽管 return 结果出现了重复的房间号和价格,如下所示
您对同一个房间有两次预订,因此返回的行与您的内部联接相匹配。您似乎正在尝试获取所有已预订的房间。您可以通过在所选字段之前添加 DISTINCTROW 来实现。
select DISTINCTROW booking.hotelno, booking.roomno,room.price
from booking
inner join room on booking.ROOMNO=room.roomno AND
booking.HOTELNO=room.HOTELNO
where booking.dateto is not null
room.roomno
不是唯一的。它仅在给定酒店内是唯一的,并且您的房间 table 包含多家酒店。您还必须在加入条件中指定 hotelno。此外,由于您可能对同一个房间有多个预订(即重复预订 table),您需要执行 DISTINCT 以防止出现这种情况(但随后您必须在字段列表中包含 hotelno 列):
select DISTINCT booking.roomno,room.price, room.hotelno
from booking
inner join room on booking.ROOMNO=room.roomno
AND booking.hotelno=room.hotelno
where booking.dateto is not null
我正在尝试列出两个属性(booking.roomno 和 room.price),条件是来自两个不同表的 "booking.DATETO is not null"。
Table:预订
我试过使用这个命令
select booking.roomno,room.price
from booking
inner join room on booking.ROOMNO=room.roomno
where booking.dateto is not null
尽管 return 结果出现了重复的房间号和价格,如下所示
您对同一个房间有两次预订,因此返回的行与您的内部联接相匹配。您似乎正在尝试获取所有已预订的房间。您可以通过在所选字段之前添加 DISTINCTROW 来实现。
select DISTINCTROW booking.hotelno, booking.roomno,room.price
from booking
inner join room on booking.ROOMNO=room.roomno AND
booking.HOTELNO=room.HOTELNO
where booking.dateto is not null
room.roomno
不是唯一的。它仅在给定酒店内是唯一的,并且您的房间 table 包含多家酒店。您还必须在加入条件中指定 hotelno。此外,由于您可能对同一个房间有多个预订(即重复预订 table),您需要执行 DISTINCT 以防止出现这种情况(但随后您必须在字段列表中包含 hotelno 列):
select DISTINCT booking.roomno,room.price, room.hotelno
from booking
inner join room on booking.ROOMNO=room.roomno
AND booking.hotelno=room.hotelno
where booking.dateto is not null