您好我有以下架构:
的酒店
hotel_code int(4) unsigned NOT NULL default '0',
hotel_name char(20) default NULL,
city char(20) default NULL,
address char(20) default NULL,
hotel_rank int(1) unsigned NOT NULL default '0',
phone char(10) default NULL,
number_of_rooms int(4) unsigned NOT NULL default '0',
PRIMARY KEY (hotel_code));
房间:
hotel_code int(4) unsigned NOT NULL default '0',
room_number int(4) unsigned NOT NULL default '0',
type char(20) default NULL,
beds int(4) unsigned NOT NULL default '0',
price int(4) unsigned NOT NULL default '0',
PRIMARY KEY (hotel_code, room_number));
现在我正在努力实现以下目标:
我的查询: 显示价格在170-380之间的房间列表 在以下方案中:
hotel_code, hotel_name, city, room_number, price
并确保按hotel_rank
和
price
我知道如何分开做两件事而不是将它们结合起来我尝试了一切......任何提示?
a = SELECT hotel_code, room_number, price FROM rooms WHERE price BETWEEN 170 AND 380 ORDER BY price;
b = SELECT hotel_code, hotel_name, city FROM hotels ORDER BY hotel_rank;
答案 0 :(得分:1)
这是一个非常基本的SQL查询,它与"更改2个表之间的模式"。
使用LEFT JOIN
(还有其他替代方法),您可以使用具有两个表共享的值的列来组合表,然后显示您需要的结果。
SELECT H.hotel_code, H.hotel_name, H.city, R.room_number, R.price
FROM hotels H LEFT JOIN rooms R ON H.hotel_code = R.hotel_code
WHERE R.price BETWEEN 170 AND 380
ORDER BY H.hotel_rank,R.price;
我建议您阅读一些基本教程(http://www.w3schools.com/sql/default.asp)