2017-05-26 38 views
0

我有1個問題在sql中如何在sql中顯示1行中的3行?

我該如何顯示3行的行?

這裏是我的SQL代碼

SELECT jobcard.code AS code1,jobcard.name AS name1,nvl(masterpm.id,0)toal 
FROM jobcard 
    LEFT JOIN masterpm ON jobcard.id = masterpm.jobcard_id 
        and masterpm.id in (select masterpm_id from cbmmasterpm) 

和圖像從選擇結果 image

現在我怎麼能顯示3特里行1行代碼,名稱不得到重複一遍,我真的很需要那

+0

我刪除了不兼容的數據庫標記。 –

+0

@GordonLinoff謝謝:) – saman

+0

請閱讀http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557和接受的答案 –

回答

1

如果你正在使用MySQL,你可以使用GROUP BYGROUP_CONCAT所有TOAL值如:

SELECT jobcard.code AS code1,jobcard.name AS name1, GROUP_CONCAT(NVL(masterpm.id,0)) AS toal 
FROM jobcard LEFT JOIN masterpm ON jobcard.id = masterpm.jobcard_id AND masterpm.id in (SELECT masterpm_id FROM cbmmasterpm) 
GROUP BY code1, name1; 

Here's關於GROUP_CONCAT的MySQL文檔。

更新

當你使用的是Oracle,你可以使用listagg函數來實現相同的功能,例如:

SELECT jobcard.code AS code1, jobcard.name AS name1, 
LISTAGG(NVL(masterpm.id,0), ', ') WITHIN GROUP (ORDER BY masterpm.id) "TOAL" 
FROM jobcard LEFT JOIN masterpm ON jobcard.id = masterpm.jobcard_id AND masterpm.id in (SELECT masterpm_id FROM cbmmasterpm) 
GROUP BY code1, name1; 

Here's的文檔。

+0

ty但我使用oracle – saman

+0

哪個版本的oracle? –

+0

oracle 11.0.2或11g – saman