SQL: select 1 与 select *

时间:2022-03-16 04:47:48

通常情况下,Select 1 from table 与slect * from table 从作用上来说是没有差别的,都是查看是否有记录,一般是作条件查询用的。select 1 from 中的1是一常量(可以为任意数值),查到的所有行的值都是它,但从效率上来说,1>anycol>*,因为不用查字典表

一些常用 sql:

select 1 from table -- 增加临时列,放在表最后, 每行值为1

select count(1) from table -- 计算表总行数

select sum(1) from table -- 计算临时增加的列的行数总和