I was recently asked this question in an interview. I tried this in mySQL, and got the same results(final results). All gave the number of rows in that particular table. Can anyone explain the major difference between them.

推荐答案

Nothing really, unless you specify a field in a table or an expression within parantheses instead of constant values or *

Let me give you a detailed answer. Count will give you non-null record number of given field. Say you have a table named A

select 1 from A
select 0 from A
select * from A

will all return same number of records, that is the number of rows in table A. Still the output is different. If there are 3 records in table. With X and Y as field names

select 1 from A will give you

1
1
1

select 0 from A will give you
0
0
0

select * from A will give you ( assume two columns X and Y is in the table )
X      Y
--     --
value1 value1
value2 (null)
value3 (null)

So, all three queries return the same number. Unless you use

select count(Y) from A 

since there is only one non-null value you will get 1 as output

Mysql相关问答推荐

如何在逗号分隔字符串值中使用LEFT函数

如何跨多个产品查找相同时间范围的数据集

当日期附加到MySQL后,如何按日期说明排序?

使用字符串文字与使用日期文字时的SQL行为

亚马逊RDS MySQL-无法';t执行';使用读取锁定刷新表';

从 NextJS/Prisma 添加用户 ID (FK) 到 MySQL 表

在 .NET 6 中使用 Dapper Framework 未能成功连接到 MySql

如何将结果列中的不同值按其他列sql中的值带入单行

了解 Spring 和数据库级别的事务?

MySQL 视图

避免重复进入mysql数据库的最佳方法

最好的 MySQL 性能调优工具?

MySql 以秒为单位的两个时间戳之间的差异?

有没有办法在 Zend Framework 1.5 中执行INSERT...ON DUPLICATE KEY UPDATE?

org.hibernate.InstantiationException:没有实体的默认构造函数::principal.Cliente

我如何决定何时使用右连接/左连接或内连接或者如何确定哪个表在哪一侧?

重新加载 .env 变量而不重新启动服务器(Laravel 5,共享主机)

mysql中float(2,2)和float()的区别

在另一个 where 语句(子查询?)中使用一个 sql 查询的结果

Yii2 如何进行 where AND 或 OR 条件分组?