考虑查询:

SELECT n.nid,
    (select count(*) as count from view_log where id = n.nid) AS the_count,
    (the_count + 1) as the_bumped_count
   FROM node n


当我运行它时,我得到Unknown column 'the_count' in 'field list'。有没有办法解决?看来the_count应该在查询中可见并且可以使用,但显然不是。顺便说一句,我也尝试了SUM(the_count, 1),但是也失败了。谢谢!

最佳答案

我认为这也可以,只需要计数一次即可:

SELECT nid, the_count, the_count+1 as the_bumped_count
FROM (
    SELECT n.nid,
        COUNT(v.*) the_count,
    FROM node n
       LEFT JOIN view_log v on n.nid = v.id
    GROUP BY n.nid
) t


或者回到您的语法:

SELECT nid, the_count, the_count+1 as the_bumped_count
FROM (
    SELECT n.nid,
        (select count(*) as count from view_log where id = n.nid) AS the_count
    FROM node n
) t


祝好运。

关于mysql - 我可以对父查询中嵌入式查询的结果进行数学运算吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14879342/

10-12 14:38