获取结果集的计数以及结果行

时间:2015-09-23 11:02:09

标签: sql sqlite

我有一个SQL查询,我需要将结果行计数并将结果作为查询的输出。

例如,

SELECT count(*) total, id, name, age, grade
FROM Employee
where grade = 10;

它打印只有一行总计数。 请让我知道查询中可能缺少的内容。

使用GROUP BY:

表:

id  name    age grade
1   ABC     30  10
2   DEF     31  10
3   GHI     29  6
4   PQR     29  10

查询:

SELECT count(*) total, id, name, age, grade
FROM Employee
where grade = 10
group by id, name, age, grade

total   id  name    age grade
1      1    ABC     30  10
1      2    DEF     31  10
1      4    PQR     29  10

总的来说,我希望有3个作为值,尽管它会被重复。 在这里,我的目的是将总行数和结果行作为单个查询的一部分。

3 个答案:

答案 0 :(得分:0)

也许你想要GROUP BY

SELECT count(*) total, id, name, age, grade
FROM Employee
where grade = 10
group by id, name, age, grade

答案 1 :(得分:0)

试试这个

    SELECT * FROM 
    ((SELECT COUNT(*) as total FROM EMPLOYEES
    WHERE grade = 10)

    CROSS JOIN
    (SELECT id, name, age, grade
    FROM Employee
    where grade = 10))

答案 2 :(得分:0)

SQLite动态计算结果行,因此在获取最后一行之前,它不知道结果集的大小。

如果可能,请更改您的程序,以便它可以计算行数。

如果您确实希望每行都有行数,则必须单独计算:

SELECT (SELECT COUNT(*)
        FROM Employee
        WHERE grade = 10
       ) AS total,
       id, name, age, grade
FROM Employee
WHERE grade = 10;