[英]Selecting distinct rows using join
我在数据库中有三个表。
旅馆
hostel_id int,
hosteltype_id int,
hostelname varchar(100)
address varchar(800)
旅馆类型
hosteltype_id int,
Hosteltypename varchar(100)
旅馆房间
room_id int,
hostel_id int,
Room_no int,
available_beds int
reserver int
旅馆中的数据
1 1 hostel1 address1
2 1 hostel2 address2
3 2 hostel3 address3
4 2 hostel4 address4
在旅馆里
1 boyshostel
2 ladieshostel
在宿舍
1 1 101 4 4
2 1 102 4 2
3 1 103 4 4
4 2 100 4 4
5 2 101 4 1
6 3 101 4 4
我可以使用命令选择行。
select Hostel.hostel_id, Hostel.hostelname, Hostel.address, hosteltypes.Hosteltypename,
from Hostel,hosteltypes
where Hostel.hosteltype_id=hosteltypes.hosteltype_id
and hostel_id = (
select distinct hostelrooms.hostel_id
from hostelrooms
where hostelrooms.hostel_id=Hostel.hostel_id and hostelrooms.hostelrooms>hostelrooms.reserver
)
i want data similar like
1 hostel1 address1 boyshostel
2 hostel2 address2 boyshostel
如何使用join语句创建与上述类似的sql命令,该命令将返回特定的hostelid,hostelname,hosteltype(如果有可用空间)。
您在SQL中缺少from子句。 应该是这样的:
SELECT Hostel.hostel_id, Hostel.hostelname, Hostel.address, hosteltypes.Hosteltypename
FROM Hostel
JOIN hosteltypes ON ( hosteltypes.hosteltype_id = Hostel.hosteltype_id )
JOIN hostelrooms ON ( hostelrooms.hostel_id = Hostelhostel_id )
假设您要尝试在hostelrooms表中获取包含条目的不同旅馆列表,则此方法应该有效。
select distinct (Hostel.hostel_id, Hostel.hostelname, Hostel.address, )hosteltypes.Hosteltypename,
from Hostel,hosteltypes,hostelrooms
where Hostel.hosteltype_id=hosteltypes.hosteltype_id
and hostel_id = hostelrooms.hostel_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.