我有3张桌子;
项目,活动,客户。
我需要返回searchterm匹配的所有3个表的结果。但是,我需要查看' name'项目和广告系列表格中的列,但'说明'客户端表中的列。注意:这是一个现有的客户端数据库,我没有权限更改列名。
示例:用户搜索'数据',我需要SELECT:
name as title FROM projects WHERE name LIKE %data%,
name as title FROM campaigns WHERE name LIKE %data%
和
description as title FROM clients WHERE description LIKE %data%
我正在努力合并查询。下面是我到目前为止,它返回语法错误。我也在想我可能采取了错误的做法。
SELECT
p.name,
c.name,
cl.description AS title
FROM
projects,
campaigns,
clients
WHERE
p.name LIKE % DATA %
OR c.name LIKE % DATA %
OR cl.description LIKE % DATA %
答案 0 :(得分:3)
您正在寻找union all
:
SELECT name as title FROM projects WHERE name LIKE %data%,
UNION ALL
SELECT name as title FROM campaigns WHERE name LIKE %data%
UNION ALL
SELECT description as title FROM clients WHERE description LIKE %data%;
如果您要删除重复项,请使用UNION
代替UNION ALL
。