我需要从表中提取一些数据
例如:
+-------------+------------+-------------+------------+
| Code | Progress | QTY | Faulty |
+-------------+------------+-------------+------------+
| A | 10 | 100 | 0 |
+-------------+------------+-------------+------------+
| B | 1 | 10 | 0 |
+-------------+------------+-------------+------------+
| C | 4 | 0 | 1 |
+-------------+------------+-------------+------------+
| D | 7 | 100 | 0 |
+-------------+------------+-------------+------------+
SELECT Code, Progress, QTY, Faulty
FROM myTable
WHERE Progress <> 7 AND Progress = 4 AND QTY > 0
如何选择所有记录 - 进展&lt;&gt; 7 - 和进度4但仅当具有进度的行的QTY大于0时 - 故障的值是1?
答案 0 :(得分:1)
请试一试
SELECT
Code,
Progress,
QTY,
Faulty
FROM myTable
WHERE Progress NOT IN(4,7)
OR (Progress = 4 AND QTY > 0 AND Faulty = 1)
<强>解释强>
Progress <> 7
之类的条件会带来Progress = 4
的记录。该特定记录可能没有QTY > 0 AND Faulty = 1
。
这就是为什么使用Progress NOT IN (4,7)
作为第一个条件。
现在你需要担心你的第二个案例,就像你提到的那样非常直接。
(Progress = 4 AND QTY > 0 AND Faulty = 1)
将Progress = 4 having QTY >0 and Faulty = 1
的记录带入最终结果集。
答案 1 :(得分:0)
select * from table
where Progress<>7
and Progress in (select Progress from table t where QTY>0 and Faulty =1)