SELECT branches.brid,
COALESCE(a.cnt, 0) AS Assigned,
COALESCE(c.cnt, 0) AS Completed,
COALESCE(p.cnt, 0) AS Pending,
COALESCE(x.cnt, 0) AS Expired
FROM branches WHERE access = 'User'
LEFT JOIN
(SELECT brid, count(*) from task GROUP BY brid) a ON branches.brid = a.brid
LEFT JOIN
(SELECT brid, count(*) from task WHERE stat = 'Completed' GROUP BY brid) c ON branches.brid = c.brid
LEFT JOIN
(SELECT brid, count(*) from task WHERE stat = 'Pending' GROUP BY brid) p ON branches.brid = p.brid
LEFT JOIN
(SELECT brid, count(*) from task WHERE stat = 'Expired' GROUP BY brid) x ON branches.brid = x.brid
ORDER BY branches.brid ASC;
我创建视图时出现此错误是否有任何方法可以执行此操作?此查询有效但我无法在我的数据库中创建视图
错误1349(HY000):视图的SELECT包含FROM子句中的子查询
答案 0 :(得分:1)
错误或多或少地解释了自己。
以下是documentation的摘录,您需要运行MySQL 5.7.7或更新版本才能在视图中使用子查询。
视图定义受以下限制:
在MySQL 5.7.7之前,SELECT语句不能包含子查询 在FROM子句中。 ....
您可以将查询重写为以下内容:
SELECT branches.brid,
COUNT(t.stat) AS Assigned,
SUM(CASE WHEN t.stat = 'Completed' THEN 1 ELSE 0 END) AS Completed,
SUM(CASE WHEN t.stat = 'Pending' THEN 1 ELSE 0 END) AS Pending,
SUM(CASE WHEN t.stat = 'Expired' THEN 1 ELSE 0 END) AS Expired
FROM branches
LEFT JOIN task t ON branches.brid = t.brid
WHERE access = 'User'
GROUP BY branches.brid
ORDER BY branches.brid ASC;