這裏是3 MySQL表格式:如何使用php和mysql從外部表中搜索數據?
project_status
psid p_id cdid sid short_list res_sent
1 3 112 24 0 0
2 3 113 25 0 1
3 4 114 22 0 1
4 4 115 23 1 0
5 5 116 26 0 0
contact_details:
cdid family_name given_name department title cid
112 xx xx xx xx 1
113 xx xx xx xx 2
114 xx xx xx xx 3
115 xx xx xx xx 4
116 xx xx xx xx 5
公司:
cid company_name
1 xx
2 xx
3 xx
4 xx
5 xx
現在,我需要從基於p_id
和sid
project_status
表中搜索cdid
顯示family_name, given_name, department, title
所以我使用下面的查詢,我得到3個結果
SELECT
cd.family_name,
cd.given_name,
cd.department,
cd.title,
cd.cid,
ps.*, company.company_name
FROM
contact_details AS cd
LEFT JOIN project_status AS ps ON cd.cdid = ps.cdid
LEFT JOIN company ON company.cid = cd.cid
WHERE
1 = 1
AND ps.sid IN (25, 22)
AND ps.p_id = '3'
ORDER BY
company.company_name ASC
NOW我想再次在上搜索結果信息(有3個結果)基於family_name or given_name or department搜索字串
對於我使用下面的查詢:
SELECT
cd.family_name,
cd.given_name,
cd.department,
cd.title,
cd.cid,
ps.*, company.company_name
FROM
contact_details AS cd
LEFT JOIN project_status AS ps ON cd.cdid = ps.cdid
LEFT JOIN company ON company.cid = cd.cid
WHERE
1 = 1
AND ps.sid IN (25, 22)
AND company.company_name LIKE '%ab%'
OR cd.family_name LIKE '%ab%'
OR cd.given_name LIKE '%ab%'
AND ps.p_id = '3'
GROUP BY
cd.cdid
ORDER BY
company.company_name ASC
現在是不應該給我更多的3的結果,因爲我有我的第一個查詢3所示的結果。但不幸的是它向我顯示contact_details table
爲什麼'WHERE 1 = 1'是什麼增加了黨 – RiggsFolly
包裝你'或'子句'()'否則只會造成混亂目前大公正在擴大搜索條件超出你的想法 – RiggsFolly
@Avishek實際上不相關 – RiggsFolly