gpt4 book ai didi

mysql像oracle中的group by一样如何

转载 作者:行者123 更新时间:2023-11-29 06:05:46 25 4
gpt4 key购买 nike

在 Mysql 中,我有 fdllowing 查询以及下面的结果

drop table tab1;
CREATE TEMPORARY TABLE tab1
(col1 integer,col2 integer(10),col3 varchar(10),col4 integer)engine=memory
insert into tab1
values(100,1,'Hello',9);
insert into tab1
values(200,1,'HelloWrld',8);
insert into tab1
values(300,1,'HelloTher',7);
insert into tab1
values(400,2,'HiThere',6);
insert into tab1
values(500,3,'Howdy',5);
insert into tab1
values(600,3,'Hiya',4);

select col1,col2,col3,col4,min(col4)
from tab1
group by col2

'100', '1', 'Hello', '9', '7'
'400', '2', 'HiThere', '6', '6'
'500', '3', 'Howdy', '5', '4'

在 Oracle 中我想要与 Mysql 相同的结果

with tab1 as (
select 100 col1, 1 col2, 'Hello' col3,9 col4 from dual
union all
select 200 col1, 1 col2, 'HelloWrld' col3,8 col4 from dual
union all
select 300 col1, 1 col2, 'HelloTher' col3,7 col4 from dual
union all
select 400 col1, 2 col2, 'HiThere' col3,6 col4 from dual
union all
select 500 col1, 3 col2, 'Howdy' col3,5 col4 from dual
union all
select 600 col1, 3 col2, 'Hiya' col3,4 col4 from dual
)
select min(col1),col2,min(col3),col4,min(col4)
from tab1
group by col2,col4

Result I get is this
MIN(COL1) COL2 MIN(COL3) COL4 MIN(COL4)
---------- ---------- --------- ---------- ----------
100 1 Hello 9 9
200 1 HelloWrld 8 8
500 3 Howdy 5 5
600 3 Hiya 4 4
300 1 HelloTher 7 7
400 2 HiThere 6 6

我想要的是这个

'100', '1', 'Hello', '9', '7'
'400', '2', 'HiThere', '6', '6'
'500', '3', 'Howdy', '5', '4'

如何在Oracle中实现类似Mysql的group by我无法得到这个,这是我试图解决的一个长查询的一部分

最佳答案

根据mysql文档,group by中未指定的列的结果可以来自任何行。

因此,Oracle 中一个完全合理的查询是:

 select min(col1),col2,min(col3),min(col4),min(col4)
from tab1
group by col2

如果您的 mysql 代码取决于所选的特定值,那么该代码就会被破坏。您需要准确地弄清楚您想要什么,并弄清楚如何在 Oracle 中获得它。

关于mysql像oracle中的group by一样如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11582653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com