带有右连接的嵌套查询出现1054错误

时间:2015-11-04 21:48:53

标签: mysql sql

我想在显示当前门票时显示每位员工的门票数量。

SELECT s.firstname, s.lastname, t.ticket_id, o.id, t.created FROM ost_ticket t
JOIN ost_staff s ON t.staff_id = s.staff_id
JOIN ost_user u ON t.user_id = u.id
JOIN ost_organization o ON u.org_id = o.id
RIGHT JOIN (
    SELECT COUNT(tt.ticket_id) 
    FROM ost_ticket tt) a ON t.ticket_id = a.ticket_id WHERE...

我在查询中遇到错误(1054):

  

未知栏' a.ticket_id'在条款'。

1 个答案:

答案 0 :(得分:1)

如果要在子查询中引用它,则需要在子查询中选择ticket_id

RIGHT JOIN 
(
  SELECT ticket_id, COUNT(ticket_id) as cnt
  FROM ost_ticket 
  GROUP BY ticket_id
) a ON t.ticket_id = a.ticket_id