我有这个查询
SELECT number_title,
(SELECT COUNT(*)
FROM poll_stemmen
) AS total_number,
(SELECT COUNT(ps.number_id)
FROM poll_stemmen
group by ps.number_id
) AS voted
FROM poll_numbers AS pn
RIGHT JOIN poll_stemmen AS ps ON ps.number_id = pn.id
GROUP BY ps.number_id
但我想在voted和total_number字段之间进行计算
所以我提出了这个问题:
SELECT number_title,
(SELECT COUNT(*)
FROM poll_stemmen
) AS total_number,
(SELECT COUNT(ps.number_id)
FROM poll_stemmen
group by ps.number_id
) AS voted, (SELECT voted) AS another_number
FROM poll_numbers AS pn
RIGHT JOIN poll_stemmen AS ps ON ps.number_id = pn.id
GROUP BY ps.number_id
但是我收到了这个错误:
不支持参考'已投票'(参考群组功能)
我该如何解决这个问题?
答案 0 :(得分:1)
您可以将查询包装在子查询中。然后在外部查询中以任何您喜欢的方式使用其别名,例如
SELECT number_title, total_number, voted,
total_number * voted AS myCalculatedColumn
FROM (
SELECT number_title,
(SELECT COUNT(*)
FROM poll_stemmen) AS total_number,
(SELECT COUNT(ps.number_id)
FROM poll_stemmen
group by ps.number_id) AS voted
FROM poll_numbers AS pn
RIGHT JOIN poll_stemmen AS ps ON ps.number_id = pn.id
GROUP BY ps.number_id ) t