0

我想知道如何使用 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);

谢谢..!

4

2 回答 2

0

您可以应用 distinct 来避免相同的多条记录,而不是 group by。因为当有聚合函数时使用 group by 是您的查询。当两行完全相同时,Distinct 检索单行而不是多行。

尝试这个

select distinct columnname from table name
union
select distinct columnname1 from table name
于 2013-04-24T07:34:33.963 回答
-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;
于 2013-04-24T04:48:12.233 回答