在复杂查询中使用union

时间:2018-03-13 05:08:44

标签: mysql sql sqlite

这是我的sql代码。当我在mysql工作台上运行它时它运行得很好但是在union不会影响最终结果的地方周围存在语法错误。我试图添加括号来分隔联合部分,但我仍然会遇到相同的语法错误。 并不是说它在我运行时似乎工作得很好。

SELECT date, time_code, number, power
FROM power_total
WHERE number IN ( 
                  SELECT number
                  FROM table_1 AS b
                  WHERE something
                    AND something
                    AND something
                    AND type = 1

                  UNION

                  SELECT dem.number AS number
                  FROM table_1 AS b
                  INNER JOIN (SELECT re.point_number AS number
                              FROM table_2 AS re
                              INNER JOIN table_3 AS dc
                                ON something = something
                              WHERE type_id IN (1 , 2)
                                 AND something IS NULL
                                 AND date <= '2016-09-01'
                            ) AS dem
                    ON dem.number = b.number
                 )
AND date BETWEEN '20160901' AND '20160930'

1 个答案:

答案 0 :(得分:-1)

  

对欲望输出使用fallowing查询:

SELECT date, time_code, number, power FROM power_total
    WHERE number IN (  SELECT number  FROM table_1 AS b 
                                      WHERE something =something        
                                      AND something=something
                                      AND something=something
                                      AND type = 1
                      UNION
                      SELECT dem.number AS number   FROM table_1 AS b
                      INNER JOIN (SELECT re.point_number AS number    FROM table_2 AS re
                                  INNER JOIN table_3 AS dc 
                                    ON something = something
                                      WHERE type_id IN (1 , 2)
                                     AND something IS NULL
                                     AND date <= '2016-09-01'
                                ) AS dem
                        ON dem.number = b.number) 
             AND date BETWEEN '20160901' AND '20160930'