如何在sql中显示3行1行?

时间:2017-05-26 20:47:41

标签: sql oracle

我在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)

和选择结果的图像 image

现在我怎么能在1行显示3行代码和代码,名字不要重复我真的需要那个

1 个答案:

答案 0 :(得分: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文档。