2014-12-01 132 views
0

我有一個搜索關鍵字,我正在使用兩個表搜索該關鍵字。用於搜索關鍵字的Mysql查詢

搜索關鍵詞的 '病人'

表 - default_pages

id  type_id  parent_id   status 
68  16   0     draft 
70  17   68    live 
227  17   44    live 
262  1   31    live 

表 - default_search

id  title     entry_id 
1  patient status  70 
2  patient check   227 
3  patient health  262 

我的查詢是

"SELECT 
      s.title, p.id, p.type_id AS sqem 

      FROM default_search s LEFT JOIN default_pages p ON p.id=s.entry_id 

      WHERE s.title LIKE '%Patient%' 

      HAVING sqem IS NOT NULL" 

上述查詢重寫在默認頁面的ID爲70,227,262的情況下會變成3個結果,但是問題在於ID 70,它的父ID是68,ID 68的狀態是草稿,所以我想從結果集中排除這一行,是我卡住的地方。

任何幫助,高度讚賞。提前致謝。

回答

0

default_pages表檢查情況:

SELECT 
s.title, p.id, p.type_id AS sqem 
FROM default_search s LEFT JOIN default_pages p ON p.id=s.entry_id 
WHERE s.title LIKE '%Patient%' AND p.status='live' 
HAVING sqem IS NOT NULL 
+0

它返回相同的3行。但我不希望70被列入結果,因爲它的父母已經獲得'草稿'狀態。 – Raj 2014-12-01 22:45:00

+0

我想你應該看看這個:http://stackoverflow.com/a/1446831/3067928並根據這個修改你的查詢。你爲什麼不創建sqlfiddle。所以,其他人也可以嘗試一下。 – 2014-12-01 22:49:27

0
SELECT 
    s.title, p.id, p.type_id AS sqem 
    FROM default_search s LEFT JOIN default_pages p ON p.id=s.entry_id 
    LEFT JOIN default_pages dp ON p.parent_id = dp.id 
    WHERE s.title LIKE '%Patient%' AND p.status='live' AND dp.status='live' 
    HAVING sqem IS NOT NULL 

另一個LEFT JOIN與default_pages 「連接」 ID爲PARENT_ID?