2016-08-01 64 views
0

這裏是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_idsidproject_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

+1

爲什麼'WHERE 1 = 1'是什麼增加了黨 – RiggsFolly

+1

包裝你'或'子句'()'否則只會造成混亂目前大公正在擴大搜索條件超出你的想法 – RiggsFolly

+0

@Avishek實際上不相關 – RiggsFolly

回答

1

嘗試修改SQL的最後一部分:

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 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 
+0

讓我現在試試。 –

+0

所以我只需要添加'()'對嗎? –

+1

是的..它似乎工作:) –

0

你OR子句expaning您的標準超過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 ps.sid IN (25, 22) 
AND company.company_name LIKE '%ab%' 
AND (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 

1=1是完全沒有必要的也是

0
SELECT cd.family_name, cd.given_name, cd.department, cd.title, cd.cid, ps.*, company.company_name 
    FROM contact_details AS cd 
    INNER JOIN project_status AS ps ON cd.cdid = ps.cdid 
    INNER JOIN company ON company.cid = cd.cid 
    WHERE ps.sid IN (25,22) AND ps.p_id = '3' 
    AND (company.company_name LIKE '%ab%' OR cd.family_name LIKE '%ab%' OR cd.given_name LIKE '%ab%') 

    ORDER BY company.company_name ASC