这是我的疑问:
从location选择location_id,location_name,display_name;
结果是:
+---------------+------------------------------------+----------------+
| location_id | location_name | display_name |
+---------------+------------------------------------+----------------+
| 1 | L1 PQR MULTIPLEX AND ENTERTAINMENT | L1 PQR |
| 2 | Cinepolis | Cinepolis KP |
| 3 | PVR FORUM MALL | PVR KP |
| 333 | PRASHANTH CINEMAS | PRASHANTH MP |
| 4555 | RAVI CINEMAS | RAVI KP |
| 323213 | ASIAN GPR MULTIPLEX | ASIAN KPHB |
| 5000721013770 | PVR CENTRAL | PVR PUNJAGUTTA |
| 5000721017325 | PVR INORBIT | PVR HITECH |
| 5000981019820 | TIVOLI CINEMAS | TIVOLI SC |
| 5300181011396 | Central Panjaguttaddd | ddd |
+---------------+------------------------------------+----------------+
10 rows in set (0.00 sec)
我还需要使用LIMIT 10进行此查询中的计数,我已尝试
从位置限制10中选择count(*)作为count,location_id,location_name,display_name;
结果是:
+-------+-------------+------------------------------------+--------------+
| count | location_id | location_name | display_name |
+-------+-------------+------------------------------------+--------------+
| 50 | 1 | L1 PQR MULTIPLEX AND ENTERTAINMENT | L1 PQR |
+-------+-------------+------------------------------------+--------------+
1 row in set (0.00 sec)
为什么只提取一条记录?
我怎样才能获得计数和记录?
答案 0 :(得分:3)
count(*)
中的select
将查询转换为聚合查询。如果没有group by
,则只返回一行。实际上,在大多数数据库中,它会返回错误,因为SQL引擎不知道如何处理剩余的列。在MySQL中,它们获得了不确定的值。
如果您想要每行的总计数,我建议您使用联接来获得结果:
select lc.cnt as count, l.location_id, l.location_name, l.display_name
from location l cross join
(select count(*) as cnt from location) lc
limit 10;
如果,实际上,您确实想要每行上有一个序号,那么您就会误解count()
。为此,请使用变量:
select (@rn := @rn + 1) as count, l.location_id, l.location_name, l.display_name
from location l cross join
(select @rn := 0) vars
limit 10;
答案 1 :(得分:0)
你在那里写的内容有两个问题。
select count(*) as count, location_id, location_name, display_name from location limit 10;
第一个你不应该限制计数! 其次,计数是"组"指令它总是给出相同的唯一编号,
你到底想要解决什么?
如果你想要每列50个(我不明白为什么你会想要这样的东西
select (select count(*) from location) as count, location_id, location_name, display_name from location limit 10;