如何使用服务器端DataTable
在页脚上创建总行数
| Name | Boy | Girl | | Boy1 | - | | | Girl1 | | - | | Boy2 | - | | | Total | 2 | 1 |
我使用此查询,但结果只显示1个数据(例如,应该是3个数据)
select name, sum(case when sex = "male" then 1 else 0 end) as boy, sum(case when sex = "female" then 1 else 0 end) as girl' from student;
谢谢
答案 0 :(得分:1)
如果您的数据库支持ROLLUP
功能,那么您可以尝试以下操作:
SELECT
name,
SUM(CASE WHEN sex = "male" THEN 1 ELSE 0 END) AS boy,
SUM(CASE WHEN sex = "female" THEN 1 ELSE 0 END) AS girl
FROM student
GROUP BY name
WITH ROLLUP
如果您的数据库不支持ROLLUP
,那么可以使用UNION
在底部添加记录总数:
SELECT
name,
SUM(CASE WHEN sex = "male" THEN 1 ELSE 0 END) AS boy,
SUM(CASE WHEN sex = "female" THEN 1 ELSE 0 END) AS girl
FROM student
GROUP BY name
UNION ALL
SELECT
'total',
SUM(CASE WHEN sex = "male" THEN 1 ELSE 0 END),
SUM(CASE WHEN sex = "female" THEN 1 ELSE 0 END)
FROM student
ORDER BY
CASE WHEN name = 'total' THEN 1 ELSE 0 END,
name