gpt4 book ai didi

mysql - 当没有 "having"子句时, "where"和 "group by"是否完全等价?

转载 作者:搜寻专家 更新时间:2023-10-30 21:41:23 25 4
gpt4 key购买 nike

通常,havingwhere 子句在没有 group by 子句时有什么区别吗(特别是在 MySQL、PostrgrSQL、Oracle 中和 MSSQL)?

select * from user where foo > 3似乎等同于从拥有 foo > 3 的用户中选择 *

最佳答案

至少有一个巨大的区别:

HAVING 子句将不使用索引:

explain select username from users having user_id = 32654456;
+----+-------------+--------+-------+---------------+---------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+---------+---------+------+--------+-------------+
| 1 | SIMPLE | users | index | NULL | PRIMARY | 32 | NULL | 661107 | Using index |
+----+-------------+--------+-------+---------------+---------+---------+------+--------+-------------+

WHERE 子句将:

explain select username from users where user_id = 32654456;
+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | SIMPLE | users | const | PRIMARY | PRIMARY | 32 | const | 1 | Using index |
+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------------+

关于mysql - 当没有 "having"子句时, "where"和 "group by"是否完全等价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963386/

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