我有两个带有btr_id及其状态的表。如何获得未付债务编号?尝试了各种与众不同,存在等的查询,但是没有运气。。也许有人有想法让我步入正轨:)
下表的结果应为债务编号= 11(未偿还债务时)
+----+---------+---------------+
| PK | debt_id | debt_state_id |
+----+---------+---------------+
| 1 | 3 | 1 |
| 2 | 3 | 2 |
| 3 | 11 | 1 |
| 4 | 15 | 1 |
| 5 | 15 | 2 |
+----+---------+---------------+
+---------------+----------+
| debt_state_id | status |
+---------------+----------+
| 1 | not paid |
| 2 | paid |
+---------------+----------+
您可以使用 not exists
select t1.debt_id from table_name t1
where t1.debt_state_id = 1
and not exists (
select 1 from table_name t2
where t1.debt_id = t2.debt_id
and t2.debt_state_id = 2
)
这table_name
是问题的第一张桌子
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句