我一直在与客户合作,并继续回到这个查询问题,试图找到解决方案,我一直在谷歌搜索,但没有找到相关的结果。
我的客户有多种旅行服务,如游轮,海滩度假村,酒店,小木屋等。
我已经更新了所有单独的搜索页面,并且一直致力于让合并后的搜索页面正常运行。
旅行服务的表格完全不相关,并且不共享许多字段名称。
在合并搜索页面上,我正在尝试根据用户搜索字词从多个表中获取搜索结果。因此,如果用户输入“夏威夷”作为搜索词,他们应该会看到夏威夷作为停靠港,夏威夷的酒店,夏威夷的海滩度假村等游轮的结果。
例如,这里有3个表格(简化,现场网站上有许多其他字段):
表格提出:
+----+--------------+-------------+-------+------+
| id | cruise_name | destination | price | deck |
+----+--------------+-------------+-------+------+
| 1 | royal cruise | Hawaii | 1000 | 1 |
| 2 | crown cruise | St. Martin | 1200 | 2 |
+----+--------------+-------------+-------+------+
表酒店:
+----+--------------+-------------+-------+------+
| id | hotel_name | location | price | beds |
+----+--------------+-------------+-------+------+
| 1 | sheraton | Hawaii | 139 | 2 |
| 2 | meriott | Florida | 75 | 2 |
+----+--------------+-------------+-------+------+
表度假村:
+----+--------------+-------------+-------+-----------+
| id | resort | location | price | room_type |
+----+--------------+-------------+-------+-----------+
| 1 | sandals | Jamaica | 2000 | 3 |
| 2 | dream resort | Hawaii | 2300 | 1 |
+----+--------------+-------------+-------+-----------+
作为一个简单的例子,假设我是用户搜索“夏威夷”,我需要向他们展示上面每个表格的结果。
我得到的最接近的是这样的查询:
SELECT
cuises.id AS cruise_id,
cuises.cruise_name AS cruise_cruise_name,
cuises.destination AS cruise_destination,
cuises.price AS cruise_price,
cuises.deck AS cruise_deck,
hotels.id AS hotels_id,
hotels.hotel_name AS hotels_hotel_name,
hotels.location AS hotels_location,
hotels.price AS hotels_price,
hotels.beds AS hotels_beds,
resorts.id AS resorts_id,
resorts.resort AS resorts_resort,
resorts.location AS resorts_location,
resorts.price AS resorts_price,
resorts.room_type AS resorts_room_type
FROM
cuises,
hotels,
vacation_packages,
resorts
WHERE
(cuises.cruise_name LIKE '%Hawaii%') OR
(hotels.hotel_name LIKE '%Hawaii%') OR
(resorts.resort LIKE '%Hawaii%')
上面的样式查询将结果混合在一起,成为包含多个表中数据的行,而我正在寻找的是包含每行只有一个表格的信息的行。
我希望psuedo-tabel从查询中看起来像这样,这样我就可以轻松地在PHP中解析结果:
+-----------+---------------------+--------------------+--------------+-------------+-----------+---------------------+--------------------+--------------+-------------+------------+----------------------+---------------------+---------------+-------------------+
| cruise_id | cruise_cruise_name | cruise_destination | cruise_price | cruise_deck | hotels_id | hotels_hotel_name | hotels_location | hotels_price | hotels_beds | resorts_id | resorts_resort | resorts_location | resorts_price | resorts_room_type |
+-----------+---------------------+--------------------+--------------+-------------+-----------+---------------------+--------------------+--------------+-------------+------------+----------------------+---------------------+---------------+-------------------+
| 1 | royal cruise | Hawaii | 1000 | 1 | | | | | | | | | | |
| 2 | crown cruise | St. Martin | 1200 | 2 | | | | | | | | | | |
| | | | | | 1 | sheraton | Hawaii | 139 | 2 | | | | | |
| | | | | | 2 | meriott | Florida | 75 | 2 | | | | | |
| | | | | | | | | | | 1 | sandals | Jamaica | 2000 | 3 |
| | | | | | | | | | | 2 | dream resort | Hawaii | 2300 | 1 |
+-----------+---------------------+--------------------+--------------+-------------+-----------+---------------------+--------------------+--------------+-------------+------------+----------------------+---------------------+---------------+-------------------+
基本上,如果我可以将搜索结果组合在多个表中,其中的字段就像上表一样放在一起,并且度假村的字段在有酒店信息的行中是空的,我可以轻松使用LIMIT和OFFSET对返回的数据进行分页,并使用PHP将返回的信息作为数组循环,并根据非空的id字段确定行中的旅行服务,以显示该行的信息使用适当的模板。
有没有办法查询多个表格,并将它们的行合并到一个不会混淆数据的结果集中?
答案 0 :(得分:1)
您想要一个union query,这将在每个表上进行单独选择,并将所有结果合并到一个查询中。使用union查询的规定是所有union'd查询必须返回相同数量的列,因此您需要从每个子查询中的所有其他表select null as fieldname
:
SELECT * FROM (
(SELECT
cuises.id AS cruise_id,
cuises.cruise_name AS cruise_cruise_name,
cuises.destination AS cruise_destination,
cuises.price AS cruise_price,
cuises.deck AS cruise_deck,
null as hotels_id, null as hotels_hotel_name, null as hotels_location, null as hotels_price, null as hotels_beds,
null as resorts_id, null as resorts_resort, null as resorts_location, null as resorts_price, null as resorts_room_type
FROM cruises
WHERE cruises.cruise_name LIKE '%Hawaii%')
UNION ALL
(SELECT
null as cruise_id, null as cruise_cruise_name, null as cruise_destination, null as cruise_price, null as cruise_deck,
hotels.id AS hotels_id,
hotels.hotel_name AS hotels_hotel_name,
hotels.location AS hotels_location,
hotels.price AS hotels_price,
hotels.beds AS hotels_beds
null as resorts_id, null as resorts_resort, null as resorts_location, null as resorts_price, null as resorts_room_type
FROM hotels
WHERE hotels.hotel_name LIKE '%Hawaii%')
UNION ALL
(SELECT
null as cruise_id, null as cruise_cruise_name, null as cruise_destination, null as cruise_price, null as cruise_deck,
null as hotels_id, null as hotels_hotel_name, null as hotels_location, null as hotels_price, null as hotels_beds,
resorts.id AS resorts_id,
resorts.resort AS resorts_resort,
resorts.location AS resorts_location,
resorts.price AS resorts_price,
resorts.room_type AS resorts_room_type
FROM resorts
WHERE resorts.resort LIKE '%Hawaii%')
)
从这里开始,您可以在此查询结束时轻松排序,过滤和限制结果。
注意:我没有在其中包含vacation_packages
表,因为您没有从中选择任何内容或在条件中使用它。要添加它,只需使用新的UNION ALL
语句添加另一个SELECT
答案 1 :(得分:1)
您的查询正在执行各种属性的笛卡尔积。您只需要一个列表,因此union all
是可行的方法。
问题是您必须为每个子选择定义所有列。呸。这需要输入显式的NULL值。以下显示了游轮和酒店的想法:
SELECT c.id AS cruise_id, c.cruise_name AS cruise_cruise_name, c.destination AS cruise_destination,
c.price AS cruise_price, c.deck AS cruise_deck,
NULL AS hotels_id, NULL AS hotels_hotel_name, NULL AS hotels_location,
NULL AS hotels_price, NULL AS hotels_beds,
. . .
FROM cruises c
WHERE cuises.cruise_name LIKE '%Hawaii%'
union all
SELECT NULL AS cruise_id, NULLAS cruise_cruise_name, NULL AS cruise_destination,
NULL AS cruise_price, NULL AS cruise_deck,
h.id AS hotels_id, h.hotel_name AS hotels_hotel_name, h.location AS hotels_location,
h.price AS hotels_price, h.beds AS hotels_beds,
. . .
FROM hotels h
WHERE hotels.hotel_name LIKE '%Hawaii%';
答案 2 :(得分:0)
我不认为你可以让mysql每个表返回1行,就像你想要的那样.. 我可能是错的,但我认为你真的不应该想要像这样返回你的数据。
我会使用LEFT JOIN
和Alaises获取所有数据,并以您正在使用的任何显示语言解析它。
这是这些表的小提琴: http://sqlfiddle.com/#!2/7ebd7d/20