SQL查询 - 比较临时表中的所有行

时间:2014-04-25 20:36:24

标签: mysql sql

我有一个表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

的ID

修改
这是MySQL。

如果Room_Nums有2行,则表示我要求2个房间符合Room_Nums + Adults ex。

Children

编辑2
要求:

  • 1间客房:2位成人和1位客人0个孩子
  • 1间客房:2位成人和1位客人5个孩子

结果应为:

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 | ----------------------------------------------- 中的行,则显示空结果。

1 个答案:

答案 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);