Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从标记列表和直接搜索中获取MySQL数据_Mysql_Sql - Fatal编程技术网

从标记列表和直接搜索中获取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是你问题的解决方案