准备一张表:t_user
首先,按年龄和性别进行分组:select id from t_user where 1=1 group by sex,age
,有 4 条结果,ID 分别为:3,5,7,9
然后,我按照子查询的方式:select * from t_user where id in (select id from t_user where 1=1 group by sex,age) order by id
,却超出了 4 条结果,而子查询体正是上一个分组查询的结果。
所以请教一下,为何把分组查询的结果放到子查询,再次查询时结果不是分组查询的结果呢?
1
JasonLaw 2021-06-10 20:37:07 +08:00 via iPhone
描述一下你的重现步骤吧,不然都是靠猜。还有,你的写法真的很奇怪。
|
2
c6h6benzene 2021-06-10 21:07:03 +08:00 via iPhone
where 1=1 是为了啥…
话说我主要写 t-sql 的,看到 group by 没有聚合函数总觉得浑身不自在。 |
3
aragakiyuii 2021-06-10 22:07:34 +08:00 via iPhone
select 的 column 不应该是在 group by 里吗?
https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard SQL use of GROUP BY permits the select list, HAVING condition, or ORDER BY list to refer to nonaggregated columns even if the columns are not functionally dependent on GROUP BY columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic, which is probably not what you want. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Result set sorting occurs after values have been chosen, and ORDER BY does not affect which value within each group the server chooses. Disabling ONLY_FULL_GROUP_BY is useful primarily when you know that, due to some property of the data, all values in each nonaggregated column not named in the GROUP BY are the same for each group. |
4
Euthpic 2021-06-10 23:37:46 +08:00 1
|
5
NotFoundEgg 2021-06-10 23:50:06 +08:00
可以考虑把子查询的结果 放在 select * from t_user where id in () 里试下有几条
|
6
T0m008 2021-06-11 07:34:41 +08:00
你这是要按照年龄性别排序么?你先描述一下,你这查询写的没什么意义
|
7
xiangyuecn 2021-06-11 09:25:06 +08:00
一看就知道是低版本 mysql,居然无理取闹的支持 select AA group by BB 😂 mysql 自己都看不下去了
|
8
sanggao 2021-06-11 09:33:25 +08:00
@xiangyuecn 高版本也支持,5.6 5.7 5.8 都支持 也有啥问题?
|
9
enderftt 2021-06-11 09:46:25 +08:00
仅有 mysql 有这种写法 你把 sex ,age 也放到结果列里面就知道了
|
10
s609926202 OP @T0m008 实际查询大概也是这个意思。
|
11
s609926202 OP @enderftt
放到结果列? |
12
s609926202 OP @NotFoundEgg 子查询结果放在 select * from t_user where id in () 里,有几个 id 就有几条结果。
|
13
s609926202 OP @c6h6benzene where 中 1=1 查询全部结果
|
14
xiangyuecn 2021-06-11 10:16:29 +08:00
@sanggao #8 说的就是 5.x == 低版本😂
|
15
c6h6benzene 2021-06-11 10:44:33 +08:00
@s609926202 主要不写 1=1 也是全部结果,除非是后续有拼接需求,不然这部分就是多此一举。
就跟 @Euthpic 说的,你这 ID 可能会有重复,你看看每个 ID 有几条就知道了: select id, COUNT(id) from t_user group by id |
16
sanggao 2021-06-11 11:49:53 +08:00
@xiangyuecn 啥意思啊 这个语法 mysql 全部版本都支持,你说的是什么版本不支持?
|
17
sanggao 2021-06-11 11:50:57 +08:00
@xiangyuecn mysql 最高版本现在就是 5.8 也支持
|