我想知道如何使用group by但仍显示完整数据?我只是想把它分组。
这里我举一个我的表的例子:
这是我的疑问:
(SELECT dp.menu_paket,d.id_detail,t.no_meja,m.nama_menu,d.jumlah,t.status,t.nama_pegawai
FROM menu m
join detail_paket dp on dp.menu_paket=m.nama_menu
JOIN detail_transaksi d on m.id_menu = d.id_menu
join transaksi t on t.id_transaksi=d.id_transaksi where t.status='progress' and d.status_menu='progress' group by id_detail)
UNION
(SELECT dp.menu_paket,d.id_detail,t.no_meja,p.nama_paket,d.jumlah,t.status,t.nama_pegawai
FROM paket p
join detail_paket dp on dp.id_paket=p.id_paket
JOIN detail_transaksi d on d.id_paket=p.id_paket
join transaksi t on t.id_transaksi=d.id_transaksi where t.status='progress' and d.status_menu='progress' group by id_detail);
感谢..!
答案 0 :(得分:0)
您可以应用distinct来避免相同的多个记录而不是group by。因为在有聚合函数的情况下使用group by是您的查询。 当两行完全相同时,Distinct将检索单行而不是多行。
试试这个
select distinct columnname from table name
union
select distinct columnname1 from table name
答案 1 :(得分:-1)
我想我看到两个问题。
1)GROUP BY
通常用于为SUM
等聚合函数分组行。您可能正在寻找控制行顺序的ORDER BY
。您可以为ORDER BY
指定多个列以获得“分组”效果。如果您只想让列在列表中彼此相邻,那么这就是您想要的。
2)UNION
,至少在我所知道的数据库中,删除了重复的行。如果要保留所有行,则需要UNION ALL
。
修改强>
回应海报的评论,你肯定想要ORDER BY
,也许UNION ALL
。它应该是ORDER BY no_meja, id_transaksi
。尝试以下查询,看看它是否能满足您的需求:
SELECT * FROM
((SELECT dp.menu_paket,d.id_detail,t.no_meja,m.nama_menu,d.jumlah,t.status,t.nama_pegawai
FROM menu m
join detail_paket dp on dp.menu_paket=m.nama_menu
JOIN detail_transaksi d on m.id_menu = d.id_menu
join transaksi t on t.id_transaksi=d.id_transaksi
where t.status='progress' and d.status_menu='progress')
UNION ALL
(SELECT dp.menu_paket,d.id_detail,t.no_meja,p.nama_paket,d.jumlah,t.status,t.nama_pegawai
FROM paket p
join detail_paket dp on dp.id_paket=p.id_paket
JOIN detail_transaksi d on d.id_paket=p.id_paket
join transaksi t on t.id_transaksi=d.id_transaksi
where t.status='progress' and d.status_menu='progress')) x
ORDER BY x.no_meja, x.id_transaksi;