我有tableA并且想要添加count colnum,count是tableA的总行,并且在结果表中的所有行都是相同的,tableA如下所示:
表A
id | name | rank |
-------------------
1 | John | 12 |
2 | Maria | 18 |
3 | Steph | 44 |
4 | Jay | 17 |
,结果应为:
id | name | rank | total | rank/total
---------------------------------------
1 | John | 12 | 4 | 3
2 | Maria | 18 | 4 | 4.5
3 | Steph | 44 | 4 | 11
4 | Jay | 17 | 4 | 4.25
如何使用MYSQL
答案 0 :(得分:0)
要获取表格中的总行数,请执行以下操作:
SELEcT count(*) FROM TableA
然后,您可以在另一个查询的SELEcT
中合并此查询并从中进行计算,从而得出:
SELEcT *, (SELEcT count(*) FROM TableA) AS total, rank / (SELEcT count(*) FROM TableA) AS 'rank/total'
FROM tableA
如果tableA是另一个查询的结果,请执行
SELEcT *, (SELEcT count(*) FROM TableA) AS total, rank / (SELEcT count(*) FROM TableA) AS 'rank/total'
FROM ( insert_the_other_query_here ) tableA
答案 1 :(得分:0)
检查使用变量是否可以帮助您:
SET @total = SELECT COUNT(1) FROM tableA;
SELECT name, rank, @total AS total, rank/@total AS 'rank/total' FROM tableA;