查看没有子查询

时间:2017-12-26 07:32:57

标签: mysql subquery

我正在尝试创建一个Mysql视图,但我们知道MySQL不允许使用子查询的视图。 如何在没有子查询的情况下创建它。有什么想法吗?

SELECT yr.users_id, yr.Godi, Inc.Prihod, exp.Rashod
FROM (
          SELECT DISTINCT YEAR(datum) Godi, users_id FROM income 
          UNION
          SELECT DISTINCT YEAR(datum) Godi, users_id FROM expense
   ) yr
LEFT JOIN (
           SELECT sum(insum) as Prihod,
                  YEAR(datum) as Godi,
                  users_id 
                  FROM income
            GROUP BY YEAR(datum), users_id
       ) inc ON yr.users_id = inc.users_id AND yr.Godi = inc.Godi 
LEFT JOIN (
           SELECT sum(amount) as Rashod,
                  YEAR(datum) as God,
                  users_id 
             FROM expense
             GROUP BY YEAR(datum), users_id
   ) exp ON yr.users_id = exp.users_id AND yr.Godi = exp.God

0 个答案:

没有答案