我有如下表我想做一个搜索:和MySQL的搜索多对多表
Table A
+----+----------------------------+
| ID | description |
+----+----------------------------+
| 0 | horse going bad |
| 1 | Older Years of Resolutions |
| 2 | The knockknock 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 A.* from tags
join taglinks on tagid=tags.id
join A on A.id=jokeid
where tag in ('list','knockknock')
让我在一个所有条目在他们的标签(或两者)中都有'列表'或'敲敲'(2,3)。我在寻找的是从A表中获取连接到BOTH列表和敲标记标记(仅限2)的条目的查询。
我也从表A,这是一件好事采取在考虑的描述直接搜索Unioning这个数据..
现在我有:
select A.* from tags
join taglinks on tagid=tags.id
join A on A.id=jokeid
where tag in ('list','knockknock')
UNION
select * from A where locate('list',description) and locate('knockknock',description)
但我也从表A中拿到3,我想只有2