在语句中使用union子查询时出错

时间:2017-09-17 04:09:35

标签: mysql

我想在' in'语句中使用where子句和子查询。

这是我的疑问:

select *
from trading.historical_prices

where
date in (

        (select date
        from trading.historical_prices
        group by date
        order by date desc
        limit 1) 

        union 
        (
        select date
        from trading.historical_prices
        group by date
        order by date desc
        limit 7,1
        ) 


)

limit 100

但是我收到了这个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union 
        (
        select date
        from trading.historical_prices
        group by date
        orde' at line 13

联合查询在单独运行时工作正常。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

根据这个answer,你没有括号。

所以你的查询看起来像这样:

SELECT * 
FROM   trading.historical_prices 
WHERE  date IN 
       ( 
                SELECT   date 
                FROM     trading.historical_prices 
                GROUP BY date 
                ORDER BY date DESC limit 1 
                UNION 
                SELECT   date 
                FROM     trading.historical_prices 
                GROUP BY date 
                ORDER BY date DESC limit 7, 
                         1) limit 100