I have 3 tables;
我有3张桌子;
projects, campaigns, clients.
项目,活动,客户。
I need to return results from all 3 tables where the searchterm matches. However, I need to check against 'name' column in projects and campaigns tables, but 'description' column in the clients table. NB This is an existing client database, I have no authority to change the the column names.
我需要返回searchterm匹配的所有3个表的结果。但是,我需要检查项目和广告系列表格中的“名称”列,而不是客户端表格中的“说明”列。 NB这是一个现有的客户端数据库,我没有权限更改列名。
Example: user searches for 'data', I need to SELECT:
示例:用户搜索'data',我需要SELECT:
name as title FROM projects WHERE name LIKE %data%,
name as title FROM campaigns WHERE name LIKE %data%
AND
和
description as title FROM clients WHERE description LIKE %data%
I'm struggling to combine the queries. Below is what I have so far, which returns a syntax error. I am also thinking I may be taking the wrong approach.
我正在努力结合查询。下面是我到目前为止,它返回一个语法错误。我也在想我可能采取了错误的做法。
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 %
1 个解决方案
#1
3
You are looking for 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%;
If you want to remove duplicates, then use UNION
instead of UNION ALL
.
如果要删除重复项,请使用UNION而不是UNION ALL。
#1
3
You are looking for 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%;
If you want to remove duplicates, then use UNION
instead of UNION ALL
.
如果要删除重复项,请使用UNION而不是UNION ALL。