我有一个名为教师的表,其中包含所有教师的详细信息,它具有first_name,last_name,email,phone_number等字段 我想得到老师的人数以及所有细节,所以我写了这样的查询
SELECT
( SELECT COUNT(*) AS count
FROM Teacher
WHERE is_disabled = false
) total_count
, id
, Teacher.*
FROM Teacher
WHERE is_disabled = 0;
它给出了正确的输出,但是当我尝试增加限制和偏移量时,计数会像
一样发生变化SELECT
( SELECT COUNT(*) AS count
FROM Teacher
WHERE is_disabled = false
) as total_count
, Teacher.*
FROM Teacher
WHERE is_disabled = 0
LIMIT 10
OFFSET 10;
因此,如果我的数据库中有9位老师,它将不会显示任何结果,但是即使没有任何限制和补偿,我也希望每次都将总数计为9
答案 0 :(得分:1)
您可以执行以下操作:
#pragma warning 1692
这将为您提供一条额外的记录,其中包含必须从GUI列表(其中is_count = 1)中要过滤的计数。坦白说,我认为您应该考虑使用2种不同的结果集。
答案 1 :(得分:1)
您可以简单地将限制和偏移量添加到子查询中:
SELECT (SELECT COUNT(*) AS count
FROM Teacher
WHERE is_disabled = 0
LIMIT 10 OFFSET 10
) as total_count,
t.*
FROM Teacher t
WHERE t.is_disabled = 0
LIMIT 10 OFFSET 10;
或者使用变量:
SELECT t.*, @rn as total_count
FROM (SELECT t.*, @rn := @rn + 1)
FROM Teacher t CROSS JOIN
(SELECT @rn := 0) params
WHERE t.is_disabled = 0
LIMIT 10 OFFSET 10
) t;