简体   繁体   中英

Understanding difference between SQL statments

I am confused on how 2 different SQL conditions would work. Suppose I have a table with 3 datetime columns named price_timestamp_a , timestamp_a and original_timestamp_a for product a and 3 datetime column pertaining to product b price_timestamp_b , timestamp_b and original_timestamp_b

I want to check for condition where price_timestamp_a < timestamp_a < original_timestamp_a is met or price_timestamp_b < timestamp_b < original_timestamp_b is met. ut the last condition should be definelty met ie cateogry <> cateogry_chunk . to achieve this I wrote the following SQL is this correct?

where (price_timestamp_a < timestamp_a
and timestamp_a < original_timestamp_a)
or (price_timestamp_b < timestamp_b
and timestamp_b < original_timestamp_b)
and (cateogry <> cateogry_chunk)

How the results from SQL above differs with the results from SQL below:

where (price_timestamp_a < timestamp_a
and timestamp_b < timestamp_original_b)
or ((price_timestamp_b < timestamp_b
and timestamp_b < original_timestamp_b)
and (cateogry <> cateogry_chunk)

Should you use as below:

WHERE cateogry <> cateogry_chunk
AND (( price_timestamp_a < timestamp_a
        AND timestamp_b < timestamp_original_b)
      OR (price_timestamp_b < timestamp_b
        AND timestamp_b < original_timestamp_b)
)

If Equality is expected, I would use:

WHERE cateogry <> cateogry_chunk
AND ( timestamp_a BETWEEN price_timestamp_a AND timestamp_original_b
      OR timestamp_b BETWEEN price_timestamp_b AND original_timestamp_b 
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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