22

有没有办法将这两个语句组合成一个而没有重复的条目?

SELECT * FROM Seq where JULIANDAY('2012-05-25 19:02:00')<=JULIANDAY(TimeP) 
         order by TimeP limit 50

SELECT * FROM Seq where JULIANDAY('2012-05-29 06:20:50')<=JULIANDAY(TimeI)  
         order by TimeI limit 50

我的第一个明显的尝试不支持SQLITE(语法错误:限制子句应该在 UNION 之后而不是之前):

SELECT * FROM Seq where JULIANDAY('2012-05-25 19:02:00')<=JULIANDAY(TimeP) 
         order by TimeP limit 50
UNION
SELECT * FROM Seq where JULIANDAY('2012-05-29 06:20:50')<=JULIANDAY(TimeI)
         order by TimeI limit 50
4

3 回答 3

37

使用子查询并在其中执行限制。

SELECT  *
FROM    (   SELECT  * 
            FROM    Seq 
            WHERE   JULIANDAY('2012-05-25 19:02:00') <= JULIANDAY(TimeP) 
            ORDER BY TimeP
            LIMIT 50
        )
UNION
SELECT  *
FROM    (   SELECT  * 
            FROM    Seq 
            WHERE   JULIANDAY('2012-05-29 06:20:50') <= JULIANDAY(TimeI) 
            ORDER BY TimeI
            LIMIT 50
        )
于 2012-05-30T08:53:44.507 回答
8

查询分阶段处理:

  1. FROM子句和所有连接;
  2. WHERE子句和所有谓词。因此,如果您想查看NULL结果集中的值,则永远不应该过滤OUTER该部分中的 -joined 表列WHERE,因为这会将您的查询变成INNER连接;
  3. GROUP BYHAVING条款;
  4. 查询组合:UNION, INTERSECT,EXCEPTMINUS
  5. ORDER BY
  6. LIMIT

因此,正如其他人指出的那样,使用ORDER BYand LIMIT before UNION子句在语法上是错误的。您应该使用子查询:

SELECT *
  FROM (SELECT * FROM Seq
         WHERE JULIANDAY('2012-05-25 19:02:00') <= JULIANDAY(TimeP) 
         ORDER BY TimeP LIMIT 50) AS tab1
UNION
SELECT *
  FROM (SELECT * FROM Seq
         WHERE JULIANDAY('2012-05-29 06:20:50') <= JULIANDAY(TimeI)  
         ORDER BY TimeI LIMIT 50) AS tab2;
于 2012-05-30T09:04:16.983 回答
2
           SELECT * from 
           (SELECT * 
           FROM Seq 
           where JULIANDAY('2012-05-25 19:02:00')<=JULIANDAY(TimeP) 
           order by TimeP limit 50)
           UNION
           SELECT * from 
           (SELECT * 
           FROM Seq 
           where JULIANDAY('2012-05-29 06:20:50')<=JULIANDAY(TimeI)
           order by TimeI limit 50)

这应该可以解决问题

于 2012-05-30T08:56:11.700 回答