gpt4 book ai didi

php - 从包含不同和不相关信息的多个表中获取 mysql 搜索结果

转载 作者:行者123 更新时间:2023-11-29 06:42:23 24 4
gpt4 key购买 nike

我一直在与客户合作,并不断回到这个查询问题,试图找到解决方案,我一直在谷歌搜索,但没有找到相关结果。

我的客户有多种旅行服务,例如邮轮、海滩度假村、酒店、小木屋等。

我已经更新了所有单独的搜索页面,并一直致力于让组合搜索页面发挥作用。

旅行服务的表完全不相关,并且不共享很多字段名称。

在组合搜索页面上,我试图根据用户搜索词从多个表中获取搜索结果。因此,如果用户输入“夏威夷”作为搜索词,他们应该会看到以夏威夷为停靠港的游轮、夏威夷酒店、夏威夷海滩度假村等的结果。

例如这里有3个表(简化了,现场还有很多其他字段):

餐 table 美食:

+----+--------------+-------------+-------+------+
| id | cruise_name | destination | price | deck |
+----+--------------+-------------+-------+------+
| 1 | royal cruise | Hawaii | 1000 | 1 |
| 2 | crown cruise | St. Martin | 1200 | 2 |
+----+--------------+-------------+-------+------+

餐 table 酒店:

+----+--------------+-------------+-------+------+
| 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 ,这将对您的每个表进行单独选择,并将所有结果连接到一个查询中。联合查询的规定是所有联合查询必须返回相同数量的列,因此您需要从每个子查询的所有其他表中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 语句

关于php - 从包含不同和不相关信息的多个表中获取 mysql 搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20692568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com