我在sql中有一个问题
如何在行中显示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)
现在我怎么能在1行显示3行代码和代码,名字不要重复我真的需要那个
答案 0 :(得分:1)
如果您使用MySQL,则可以使用GROUP BY
和GROUP_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
。
<强>更新强>
在使用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文档。