从标记列表和直接搜索中获取MySQL数据
我正在数据库中搜索。同时我安装了一个标签系统。并非所有内容都已标记,因此我还需要数据库中的“老式”结果。让我说清楚:从标记列表和直接搜索中获取MySQL数据,mysql,sql,Mysql,Sql,我正在数据库中搜索。同时我安装了一个标签系统。并非所有内容都已标记,因此我还需要数据库中的“老式”结果。让我说清楚: Table A +----+----------------------------+ | ID | description | +----+----------------------------+ | 0 | horse going bad | | 1 | Older Years of Resolutions | |
Table A
+----+----------------------------+
| ID | description |
+----+----------------------------+
| 0 | horse going bad |
| 1 | Older Years of Resolutions |
| 2 | The pirate |
| 3 | The Wish list |
| 4 | list that's no list |
+----+----------------------------+
table TAGS
+----+------------+
| ID | tag |
+----+------------+
| 0 | list |
| 1 | knockknock |
+----+------------+
table TAGLINKS
+-------+--------+
| TAGID | JOKEID |
+-------+--------+
| 0 | 2 |
| 0 | 3 |
+-------+--------+
当我执行此搜索时:
select * from A where locate('list',description)
select * from tags
join taglinks on tagid=tags.id
join A on A.id=jokeid
where tag='list'
我将从表A中获取ID 3和ID 4,这很好
当我执行此搜索时:
select * from A where locate('list',description)
select * from tags
join taglinks on tagid=tags.id
join A on A.id=jokeid
where tag='list'
我从表A中得到ID 2和3
我要取回的是身份证2、3和4。这是两个结果的结合。我试过这个:
select * from tags
join taglinks on tagid=tags.id
join A on A.id=jokeid or locate('list',description)
它似乎给了我正确的结果,但是它太慢了以至于阻塞了服务器(实际上,表比这里的示例要大得多)。我想要组合查询的原因是我需要orderby和LIMIT之类的函数。因此,我希望从上述两个查询中得到综合结果 正如Eugen Rieck和Raheel Shan所指出的,答案是联盟:
select A.* from tags
join taglinks on tagid=tags.id
join A on A.id=jokeid
where tag="list"
UNION
select * from A where locate("list",description)
给我ID 2、3和4是什么阻止你只
UNION
ing这两个快速查询?UNION是你问题的解决方案