我有一个表Rooms
和一个临时表Room_Nums
Rooms
表的列:
Rooms(
ID,
room_name,
max_Adults,
max_Children
);
-- Populate
INSERT INTO Rooms VALUES (1,"Room A",2,1),(2,"Room B",2,5),(3,"Room C",3,1);
临时表:
DROP TEMPORARY TABLE IF EXISTS Room_Nums;
CREATE TEMPORARY TABLE Room_Nums(
ID int(1),
Adults int(2),
Children int(2)
);
-- Populate
INSERT INTO Room_Nums VALUES (1,2,0),(2,2,5);
我想从{的所有行中选择仅 Rooms
+ max_adults
更小或相等(max_children
)的<=
{1}}以及来自Room_Nums
修改
这是MySQL。
如果Room_Nums
有2行,则表示我要求2个房间符合Room_Nums
+ Adults
ex。
Children
编辑2
要求:
结果应为:
SELECT r.room_name, rn.Adults, rn.Children, rn.ID
FROM Rooms r
OUTER JOIN Room_Nums rn
ON (rn.Adults <= r.max_Adults AND rn.Children <= r.max_Children )
如果找不到-----------------------------------------------
| Room Name | Adults | Children | Room_Nums.ID |
-----------------------------------------------
| Room A | 2 | 0 | 1 |
-----------------------------------------------
| Room B | 2 | 5 | 1 |
-----------------------------------------------
| Room B | 2 | 5 | 2 |
-----------------------------------------------
中的行,则显示空结果。
答案 0 :(得分:1)
虽然,现在还不是很清楚,&#34;所有的行&#34;是什么意思,也许这会起作用
Select room_name, max_adults + max_children, Room_nums.ID
from Rooms
left join Room_nums
on Rooms.id=Room_nums.id
WHERE max_adults + max_children <= (select min(adults + children) from Room_nums);