我有如下表我想做一個搜索:和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