2013-01-09 77 views
0

我正在我的數據庫中進行搜索。同時我還安裝了標籤系統。並非所有東西都被標記了,所以我也需要從我的數據庫中得到'老式'結果。讓我說清楚:從標籤列表和直接搜索中獲取MySQL數據

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) 

我會得到ID 3和4表A,這是偉大的。

當我這樣做搜索:

select * from tags 
    join taglinks on tagid=tags.id 
    join A on A.id=jokeid 
where tag='list' 

我從表A獲得ID 2和3

我想要得到的回覆是ID 2,3和4。因此,一個加入的兩個結果。我嘗試這樣做:

select * from tags 
    join taglinks on tagid=tags.id 
    join A on A.id=jokeid or locate('list',description) 

這似乎給我正確的結果,但它這麼慢呢會堵塞服務器(在現實中的表都遠遠超過例子更大這裏)。我想組合查詢的原因是我需要像ORDER BY和LIMIT這樣的函數。所以我期待從上述兩個查詢中獲得綜合結果。

+1

從什麼'只是在UNION'ing兩個快速查詢阻止你? –

+1

工會是你的問題的解決方案 –

+0

的確,是!謝謝! – patrick

回答

1

正如尤金裏克和Raheel山指出,答案是工會:

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