简体   繁体   English

SQL 同表多列查询where子句

[英]SQL Query where clause for multiple columns in same table

Having the below database:具有以下数据库:

数据库

Trying to execute below query:尝试执行以下查询:

select * 
from data_five_minutes 
where ((open in (select open 
                 from data_five_minutes 
                 where date_time = '2019-01-02 13:15:00')) > 
        (open in (select open 
                  from data_five_minutes 
                  where date_time = '2019-01-01 11:10:00')))

The above query gives me result give me 3 results but I was expecting 0!上面的查询给了我结果给我 3 个结果,但我期待 0!

What I try to fetch is:- get the row where the open of 02/01 13:15 > open of 01/01 11:10 having script_id same for both.我尝试获取的是:- 获取 02/01 13:15 开盘 > 01/01 11:10 开盘且两者的 script_id 相同的行。

You can use EXISTS :您可以使用EXISTS

SELECT d1.* 
FROM data_five_minutes d1 
WHERE d1.date_time = '2019-01-02 13:15:00'
  AND EXISTS (
    SELECT 1
    FROM data_five_minutes d2
    WHERE d2.date_time = '2019-01-01 11:10:00'
      AND d2.script_id = d1.script_id
      AND d2.open < d1.open
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM