[英]mysql: combine two tables that have NOTHING to do with each other?
我有一张桌子:
**blog_posts**
id
title
desc
post
还有一个完全不同的表,它与第一个表无关:
**devices**
id
brand
model
desc
price
我正在搜索页面上,我需要用户能够搜索并从两个表中获取结果。
我将如何编写查询以从所有字段中获取信息?
使用UNION
SELECT * FROM blog_posts WHERE `__` = 'searchparameter'
UNION
SELECT * FROM devices WHERE `__` = 'searchparameter'
条件:两个表应具有相同的列数或从两个表中选择相同的列数。
从两个表中选择的一种方法是使用union all
:
select id, title, desc, post,
NULL as device_id, null as brand, null as model, null as device_desc, null as price
from blog_posts
union all
select NULL, NULL, NULL, NULL, id, brand, model, desc , price
from devices;
您最好查看类似Elastic Search之类的解决方案。
这个要求是错误的。 当然,您可以使用UNION ALL
和类似的东西以某种方式“合并”这些表,但这是绝对错误的。 结果将是什么-设备或blog_posts或...什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.