我有这个查询:
SELECT *
FROM gross_profit
WHERE client_name = 'john'
AND timestamp = 'lm153-1531439443738'
这是结果:
+--------+-------------+---------------------+-------------+------------------+------------------+
| id | client_name | timestamp | profit_loss | profit_loss_unit | profit_loss_type |
+--------+-------------+---------------------+-------------+------------------+------------------+
| 500392 | john | lm153-1531439443738 | 1.90000000 | ADA | profit |
| 500393 | john | lm153-1531439443738 | 0.00210900 | USDT | fraction |
| 500394 | john | lm153-1531439443738 | 0.00000000 | BTC | normal |
| 509189 | john | lm153-1531439443738 | -1.43713843 | ADA | fee |
+--------+-------------+---------------------+-------------+------------------+------------------+
4 rows in set (0.01 sec)
现在,我要消除任何具有timestamp
的{{1}}。
所以我将查询修改为此:
profit_loss_type = fee
当然还有结果:SELECT *
FROM gross_profit
WHERE client_name = 'john'
AND timestamp = 'lm153-1531439443738'
GROUP BY timestamp
HAVING SUM(CASE WHEN profit_loss_type = 'fee' THEN 1 ELSE 0 END) = 0
但是为什么,当我将该查询用作子查询时:
Empty set (0.01 sec)
它给了我:SELECT * FROM (
SELECT timestamp
FROM gross_profit
WHERE client_name = 'john'
AND profit_loss > 0
GROUP BY timestamp
HAVING SUM(CASE WHEN profit_loss_type = 'fee' THEN 1 ELSE 0 END) = 0
) AS t1
WHERE t1.timestamp = 'lm153-1531439443738'
和1 row in set (0.60 sec)
显示为结果。
这真的使我感到困惑。为什么当我用作单个查询和子查询时结果不同?
答案 0 :(得分:1)
我认为这是由于WHERE条件造成的:
AND profit_loss > 0
在
然后,“费用” profit_loss类型之前已被过滤掉,结果不再为空。
尝试删除此行。