SQL count(*)只返回一行

时间:2017-10-15 07:15:03

标签: mysql sql

我是mysql的初学者。以下代码看起来很愚蠢,但这就是我的代码无法工作的原因。我以为它会给我 n 行。但是,它只给了我一排。假设表seat中有 n 行,并且有两个字段,包括 id student

我理解count(*)将返回一个数字。我认为,对于每一行,sql将检查id是否等于总行数。但它没有。

select id = count(*) as id, student 
from seat

以下代码符合我的要求。谁能解释是什么让他们得到不同的结果?

select id = count2 as id, student 
from seat, (select count(*) as count2 from seat) seat2

4 个答案:

答案 0 :(得分:0)

我认为你应该尝试这样的事情:

select id = (select count(*) from seat) as id, student 
from seat

基本上你在FROM子句(select count(*) as count2 from seat)中使用此语句的先前请求只给你一行,因此它不能与你的所有行进行比较,因为你把它放在你的FROM子句中两个表座位和seat2没有相同的行数。

我认为你不需要使用两张桌子。

答案 1 :(得分:0)

一种方式:

set @num := (select count(*) from seat);

select
      id
    , student
    , case when id = @num then 'special' else 'normal' end as x
from seat

另一种方式:

select
      id
    , student
    , case when id = x.y then 'special' else 'normal' end as x
from seat
cross join (select count(*) as y from seat) as x
;

NB :在这两个示例中,您需要子查询在一行中只返回一个值。

答案 2 :(得分:0)

鉴于

+----+----------+
| id | lastname |
+----+----------+
|  1 | aaa      |
|  2 | bbb      |
|  3 | ccc      |
+----+----------+
3 rows in set (0.00 sec)

您的第一个查询是指向mysql的指令,以计算整个集合

MariaDB [sandbox]> select id = count(*) as id, lastname
    -> from users;
+------+----------+
| id   | lastname |
+------+----------+
|    0 | aaa      |
+------+----------+
1 row in set (0.00 sec)

显然,这不会返回最后一个ID或正确的计数。此

将返回正确的计数
select  id, lastname , count(*)
from users;
+------+----------+----------+
| id   | lastname | count(*) |
+------+----------+----------+
|    1 | aaa      |        3 |
+------+----------+----------+
1 row in set (0.00 sec)

id和lastname是不确定的。

您的第二个查询会返回笛卡尔积

select id = count2, lastname, count2
    -> from users, (select count(*) as count2 from users) seat2
    -> ;
+-------------+----------+--------+
| id = count2 | lastname | count2 |
+-------------+----------+--------+
|           0 | aaa      |      3 |
|           0 | bbb      |      3 |
|           1 | ccc      |      3 |
+-------------+----------+--------+
3 rows in set (0.00 sec)

它再次无法识别与计数相匹配的ID ..

假设id是一个数字,并以某种方式递增,找到最后一个id的方式是

MariaDB [sandbox]> select id,lastname
    -> from users
    -> where  id = (select count(*) from users);
+----+----------+
| id | lastname |
+----+----------+
|  3 | ccc      |
+----+----------+
1 row in set (0.00 sec)

但这很危险 - 如果id是auto_increment,那么行数可能与id不匹配,因为处理auto_increment的方式(可以覆盖它,在重复键上插入更新等) 更安全的方法是

MariaDB [sandbox]> select id,lastname
    -> from users
    -> where  id = (select max(id) from users);
+----+----------+
| id | lastname |
+----+----------+
|  3 | ccc      |
+----+----------+
1 row in set (0.00 sec)

答案 3 :(得分:0)

  

我认为[count(*)]会给我n行。但是,它只给了我一排。

不。您使用的COUNT(*)没有GROUP BY。这使您的查询成为聚合查询。没有GROUP BY 总是的聚合查询会返回一行 - 即使表中没有行也是如此。

您的查询几乎在任何其他数据库(以及MySQL未来版本的默认配置)中都无效,因为您有一个聚合查询,id列不在GROUP BY中聚合函数的参数。

在MySQL中,可能是执行您想要使用子查询的最简单方法:

select id = (select count(*) from seat) as id, student 
from seat

我不知道您为什么要调用布尔结果id,但这就是原始查询所表达的内容。

结论:您需要练习聚合查询并查找GROUP BY所做的事情。

相关问题