SQLite 中的 SELECT *, COUNT(*) [英] SELECT *, COUNT(*) in SQLite
问题描述
如果我在 SQLite 中执行标准查询:
If i perform a standard query in SQLite:
SELECT * FROM my_table
我按预期获得了表中的所有记录.如果我执行以下查询:
I get all records in my table as expected. If i perform following query:
SELECT *, 1 FROM my_table
我按预期获得所有记录,所有记录中最右侧的列都为1".但是如果我执行查询:
I get all records as expected with rightmost column holding '1' in all records. But if i perform the query:
SELECT *, COUNT(*) FROM my_table
我只得到一行(最右边的列是正确的计数).为什么是这样的结果?我不太擅长 SQL,可能会出现这种行为?这对我来说似乎很奇怪和不合逻辑:(.
I get only ONE row (with rightmost column is a correct count). Why is such results? I'm not very good in SQL, maybe such behavior is expected? It seems very strange and unlogical to me :(.
推荐答案
SELECT *, COUNT(*) FROM my_table
不是你想要的,它不是真正有效的 SQL,你必须分组由非聚合的所有列组成.
SELECT *, COUNT(*) FROM my_table
is not what you want, and it's not really valid SQL, you have to group by all the columns that's not an aggregate.
你会想要类似的东西
SELECT somecolumn,someothercolumn, COUNT(*)
FROM my_table
GROUP BY somecolumn,someothercolumn
这篇关于SQLite 中的 SELECT *, COUNT(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!