2015-05-04 95 views
0

我寫了兩個查詢以不同條件連接值,兩個查詢的結果應該是一樣的,但是我無法用query2得到正確結果。兩個查詢如下:group_concat重複值

query1:

SELECT group_concat(concat(concat(concat(concat(concat(f.NAME, ';') , sgk.NAME),' ') ,cc.operator),' ')) as res_string 
FROM complex_check_anag cc,lnksinglechecktocomplexcheck lk,single_check_anag sgk,functionalci f ,lnkconfigurationitemtosinglecheck lkcg 
WHERE cc.complex_check_id = lk.complex_check_id AND sgk.single_check_id = lk.single_check_id and f.id = lkcg.config_item_id 
and sgk.single_check_id = lkcg.single_check_id and sgk.status = 'active' GROUP BY cc.NAME 

與QUERY1我可以得到以下結果:

res_string 
---------------------------------------------------------------------------------------------------------- 
MIL04DNS01;memory check and ,MIL04DNS01;cpu_check and 
MIL04APPBOXIP01;cpu_check and ,MIL04APPBOXIP01;memory check and 
Sito_Saturn_Secure;log in check and 

QUERY2:

SELECT group_concat(concat(concat(concat(concat(concat(f.NAME, ';') , sgk.NAME),' ') ,cc.operator),' ')) as res_string 
FROM complex_check_anag cc,lnksinglechecktocomplexcheck lk,single_check_anag sgk,functionalci f ,lnkconfigurationitemtosinglecheck lkcg,comp_t_anag cmt 
WHERE cc.complex_check_id = lk.complex_check_id AND sgk.single_check_id = lk.single_check_id and f.id = lkcg.config_item_id 
and sgk.single_check_id = lkcg.single_check_id and sgk.status = 'active' and cc.complex_check_id <> cmt.comp_o_id GROUP BY sgk.NAME 

與QUERY2我可以得到以下結果:

res_string 
------------------------------------------------------------------------------------------------------------------------------- 
MIL04DNS01;memory check and ,MIL04DNS01;cpu_check and ,MIL04DNS01;memory check and ,MIL04DNS01;cpu_check and ,MIL04DNS01;memory check and ,MIL04DNS01;cpu_check and 
MIL04APPBOXIP01;cpu_check and ,MIL04APPBOXIP01;memory check and ,MIL04APPBOXIP01;cpu_check and ,MIL04APPBOXIP01;memory check and ,MIL04APPBOXIP01;cpu_check and ,MIL04APPBOXIP01;memory check and 
Sito_Saturn_Secure;log in check and ,Sito_Saturn_Secure;log in check and ,Sito_Saturn_Secure;log in check and and ,MIL04DNS01;memory check and ,MIL04APPBOXIP01;memory check and 

你能給我一些suffestions修改QUERY2獲得與QUERY1相同的結果...感謝了很多?

+4

僅供參考,我甚至不考慮在查詢,只要他們被格式化這樣的期待。其實他們根本沒有格式化。當你處理它時,你可以熟悉1992年制定的聯合語法,並立即使用它。這只是一個難以理解的混亂。 – fancyPants

+0

您能否提供帶有數據樣本的sqlfiddle? – Alex

回答

1

這裏有一些建議給你:你的願望

  1. CONCAT函數接受盡可能多的參數,所以你不需要concat(concat(concat...

  2. 你不應該使用FROM tbl1,tbl2,tbl3...JOIN表。它應該通過LEFT JOIN,RIGHT JOININNER JOINON規則明確設置。

  3. 這裏我猜你的查詢應該如何。但我有一些問題需要了解最後一張表comp_t_anag cmt必須加入。我沒有看到加入該表的真正關鍵。而且我對所有表格都做了LEFT JOIN,也許你需要INNER某處,所以你可以玩。

SELECT GROUP_CONCAT(CONCAT(f.NAME, ';', sgk.NAME,' ',cc.operator,' ')) as res_string 
FROM complex_check_anag cc 
LEFT JOIN lnksinglechecktocomplexcheck lk 
ON cc.complex_check_id = lk.complex_check_id 
LEFT JOIN single_check_anag sgk 
ON sgk.single_check_id = lk.single_check_id 
    AND sgk.status = 'active' 
LEFT JOIN lnkconfigurationitemtosinglecheck lkcg 
ON sgk.single_check_id = lkcg.single_check_id 
LEFT JOIN functionalci f 
ON f.id = lkcg.config_item_id 
LEFT JOIN comp_t_anag cmt 
ON cc.complex_check_id <> cmt.comp_o_id 
GROUP BY sgk.NAME 
+0

好的,我明白了。非常感謝 ! –