我一直在与客户合作,并不断回到这个查询问题,试图找到解决方案,并且我一直在谷歌上搜索,但尚未找到相关结果。
我的客户有多种旅行服务,例如游轮、海滩度假村、酒店、小屋等。
我已经更新了所有单独的搜索页面,并且一直致力于使组合搜索页面发挥作用。
旅行服务的表完全不相关,并且不共享许多字段名称。
在组合搜索页面上,我尝试根据用户搜索词从多个表中获取搜索结果。因此,如果用户输入“夏威夷”作为搜索词,他们应该会看到以夏威夷为停靠港的游轮、夏威夷酒店、夏威夷海滩度假村等的结果。
例如,这里有 3 个表(简化后,实时站点上还有许多其他字段):
餐 table 菜肴:
+----+--------------+-------------+-------+------+
| 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 |
+----+--------------+-------------+-------+------+
餐 table 度假村:
+----+--------------+-------------+-------+-----------+
| 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 字段确定该行中的旅行服务以显示该行的信息使用正确的模板。
有没有一种方法可以查询多个表格,并将它们的行合并到一个结果集中,并且不会混淆数据?
最佳答案
你想要一个union query ,这将对每个表进行单独选择,并将所有结果连接到一个查询中。联合查询的规定是所有联合查询必须返回相同数量的列,因此您需要从每个子查询中的所有其他表中选择 null 作为字段名:
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
表,因为您没有从中选择任何内容或在条件中使用它。要添加它,只需添加另一个带有新 SELECT
语句的 UNION ALL
关于php - 从包含不同且不相关信息的多个表中获取 mysql 搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20692568/