Php 用于搜索关键字的Mysql查询
我有一个搜索关键字,我正在使用两个表搜索该关键字 搜索关键字为“患者” 表-默认页面Php 用于搜索关键字的Mysql查询,php,mysql,join,Php,Mysql,Join,我有一个搜索关键字,我正在使用两个表搜索该关键字 搜索关键字为“患者” 表-默认页面 id type_id parent_id status 68 16 0 draft 70 17 68 live 227 17 44 live 262 1 31
id type_id parent_id status
68 16 0 draft
70 17 68 live
227 17 44 live
262 1 31 live
表-默认搜索
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的状态是draft,因此我想从结果集中排除此行,这就是我遇到的问题
非常感谢您的帮助。提前感谢。检查默认页面表中的状态:
另一个带有默认\u页面的左连接,连接父\u id和id?它返回相同的3行。但我不希望70包含在结果中,因为它的父级已处于“草稿”状态。我认为您应该研究一下这一点:并根据这一点修改您的查询。为什么不创建SQLFIDLE呢。所以,其他人也可以试试。
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
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