Php 从包含不同和不相关信息的多个表中获取mysql搜索结果
我一直在与一位客户合作,并不断回到这个查询问题,试图找到一个解决方案,我一直在谷歌搜索,但没有找到相关的结果 我的客户有多种旅游服务,如游轮、海滩度假村、酒店、船舱等 我已经更新了所有单独的搜索页面,并一直在努力使组合搜索页面正常工作 旅行社的表是完全不相关的,并且没有共享许多字段名 在组合搜索页面上,我尝试根据用户搜索词从多个表中获取搜索结果。因此,如果用户输入“夏威夷”作为搜索词,他们应该会看到夏威夷作为停靠港的邮轮、夏威夷的酒店、夏威夷的海滩度假村等的搜索结果 例如,这里有3个表(简化后,live站点上还有许多其他字段): 表3:Php 从包含不同和不相关信息的多个表中获取mysql搜索结果,php,mysql,Php,Mysql,我一直在与一位客户合作,并不断回到这个查询问题,试图找到一个解决方案,我一直在谷歌搜索,但没有找到相关的结果 我的客户有多种旅游服务,如游轮、海滩度假村、酒店、船舱等 我已经更新了所有单独的搜索页面,并一直在努力使组合搜索页面正常工作 旅行社的表是完全不相关的,并且没有共享许多字段名 在组合搜索页面上,我尝试根据用户搜索词从多个表中获取搜索结果。因此,如果用户输入“夏威夷”作为搜索词,他们应该会看到夏威夷作为停靠港的邮轮、夏威夷的酒店、夏威夷的海滩度假村等的搜索结果 例如,这里有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 |
+----+--------------+-------------+-------+------+
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选项卡在查询中看起来像这样,这样我就可以轻松地用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查询的规定是,所有union查询必须返回相同数量的列,因此您需要从每个子查询中的所有其他表中选择null作为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\u包
因为您没有从中选择任何内容或在特定条件下使用它。要添加它,只需使用一个新的SELECT
语句添加另一个UNION ALL
,您的查询是对各种属性的笛卡尔乘积。您只需要一个列表,因此,union all
是一个不错的选择
问题是您必须为每个子选择定义所有列。讨厌。这需要输入显式的空值。下面展示了邮轮和酒店的理念:
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%';
我不认为mysql可以像您希望的那样为每个表返回1行。。 我可能错了,但我不认为你真的应该希望你的数据像那样返回 我将使用
LEFT JOIN
和alaise获取所有数据,并用您正在使用的任何显示语言对其进行解析
这是这些桌子的提琴:
谢谢!我将使用您的示例更新我的开发站点上的查询。此外,我还复制/粘贴了实际查询的一部分,并在用示例数据替换表名时错过了休假包。@rmmoul我忘记了重要部分-每个查询必须具有相同的列数,您需要为每个表中不存在的字段别名指定空值-我已经更新了答案。@scrowler的UNION ALL建议做到了这一点,还允许我将类似字段推送到共享别名中(例如,所有目标信息都位于一个字段名下)。将一些feild推到一个别名中,并为联盟生成的表提供一个临时名称,我还可以按项目名称(包含邮轮公司名称、度假村名称等,取决于从中提取的表)进行订购,这允许我按字母顺序或按价格等混合显示结果。