用于搜索关键字的Mysql查询

时间:2014-12-01 22:36:43

标签: php mysql join

我有一个搜索关键字,我正在使用两个表搜索该关键字。

搜索关键字为“患者”

表格 - 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"

上面的查询返回3个结果,其中默认页面的ID为70,227,262,但问题是id为70,它的父ID是68,而id 68的状态是草稿,所以我想排除这一行从结果集中,这就是我被困住的地方。

非常感谢任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

使用default_pages的另一个Left连接"连接" parent_id与id?

相关问题