我的数据库中有三个表格如下:
kill_list
+-----------------------------------------------+
| id | batch_name | execution_date | status |
+-----------------------------------------------+
| 1 |Shallow Minds | 2013-10-29 | 0 |
+-----------------------------------------------+
| 2 | Brain Dead | 2013-10-26 | 1 |
+-----------------------------------------------+
kill_list_designation
+------------------------------+
| id | kill_list | target |
+------------------------------+
| 1 | 1 | 1 |
+------------------------------+
| 2 | 1 | 2 |
+------------------------------+
| 3 | 2 | 4 |
+------------------------------+
| 4 | 1 | 5 |
+------------------------------+
| 5 | 2 | 3 |
+------------------------------+
targets
+--------------------------------+
| id | firstname | lastname |
+--------------------------------+
| 1 | John | Smith |
+--------------------------------+
| 2 | Mark | Hall |
+--------------------------------+
| 3 | Kim | Stone |
+--------------------------------+
| 4 | Rob | Reeds |
+--------------------------------+
| 5 | Ivan | Gay |
+--------------------------------+
现在,我只能使用此查询搜索kill_list表上的查询
SELECT *
FROM kill_list
WHERE batch_name LIKE CONCAT('%', 'query term', '%')
OR execution_date LIKE CONCAT('%', 'query term')
OR status LIKE CONCAT('%', 'query term', '%');
在此查询中,我只能获取kill列表的详细信息,但不能获取列表中包含的目标。如果我搜索“标记”,则查询应该向我提供kill_list
#1的详细信息,其中包含标记。
如何通过输入术语来获取杀戮名单的所有目标?查询必须能够将术语与目标名称进行比较。非常感谢你!
答案 0 :(得分:1)
您必须为它创建加入。
此结构的简单连接如下,
SELECT kl.*, kld.*,t.* FROM kill_list as kl,kill_list_designation as kld,targets as t
WHERE
kl.id = kld.kill_list
AND
kld.target = t.id
要专门搜索标记,您可以使用
SELECT kl.*, kld.*,t.* FROM kill_list as kl,kill_list_designation as kld,targets as t
WHERE
kl.id = kld.kill_list
AND
kld.target = t.id
AND
t.firstname like 'Mark'
答案 1 :(得分:0)
试试这个
SELECT *
FROM kill_list A
JOIN kill_list_designation B ON A.id=B.kill_list
JOIN targets C ON B.target=C.id
WHERE A.batch_name LIKE CONCAT('%', 'query term', '%')
OR A.execution_date LIKE CONCAT('%', 'query term')
OR A.status LIKE CONCAT('%', 'query term', '%')
OR C.firstname LIKE CONCAT('%', 'query term', '%')
OR C.laststname LIKE CONCAT('%', 'query term', '%');