2017-10-15 161 views
-3

我是mysql的初学者。下面的代码看起来很愚蠢,但这就是为什么我的代码无法工作。我以为它会给我n行。但是,它只给了我一行。假设有n行在表seat,并且有两个字段,包括id,学生SQL count(*)只返回一行

我明白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 
+0

你想达到什么目的? – user1506104

+0

为什么它只给你一行,请点击这里查看:http://www.mysqltutorial.org/mysql-count – user1506104

+1

您应该在教程 –

回答

0

我想你应该尝试这样的事:

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

基本上你以前在这个声明要求FROM子句(select count(*) as count2 from seat)只给你行,以便它无法比拟的所有行因为你在没有相同行数的情况下将它放在你的FROM子句中的两个表的座位和座位2上。

我认为你不需要使用两个表。

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:在这两个例子中,你需要的子查询一行只返回一个值。

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再行数可能不匹配,因为这样AU的id to_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) 
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的功能。