INNER JOIN计数ON子句中的Unknown列

时间:2016-10-13 14:10:44

标签: sql inner-join

我正在使用HeidiSQl并尝试运行查询,我从表1中获取所有用户,然后计算表2中所有匹配的用户。

SELECT DISTINCT problem, title, username, createdate, expiredate
FROM solutionhistory as table1
INNER JOIN (SELECT COUNT(LOGIN_NAME) FROM ra_report_table) as table2
ON table1.username=table2.LOGIN_NAME
WHERE username != 'test'
LIMIT 10

我有点陷入困境!我在table2.LOGIN_NAME的on子句中获取了错误未知列。

如果我引用了COUNT(LOGIN_NAME),那么查询就有效 - 如果有人能指出我的方向很好!

1 个答案:

答案 0 :(得分:2)

您的查询在很多级别上没有意义。我猜你想要更像这样的东西:

SELECT problem, title, username, createdate, expiredate, table2.cnt
FROM solutionhistory as table1 INNER JOIN
     (SELECT LOGIN_NAME, COUNT(LOGIN_NAME) as cnt
      FROM ra_report_table
      GROUP BY LOGIN_NAME
    ) as table2
    ON table1.username = table2.LOGIN_NAME
WHERE username <> 'test'
LIMIT 10