首页 > 解决方案 > PostgreSQL 全部

问题描述

我有以下表结构:

| id | object_id | status_id |
------------------------------
| 1  |     12    |     1     |
| 2  |     12    |     2     |
| 3  |     18    |     5     | 

我需要选择所有object_id处于状态 1 和 2 的状态。也就是说,像这样:select object_id from table_name where status_id in (1, 2),但我需要status_id不在列出的值之一中,而在两者中。也就是说,从上表中,我应该返回值 12(因为object_idstatuses 等于 12)。如何才能做到这一点?

标签: sqlpostgresqlrelational-division

解决方案


如果您只需要状态 1 或 2 中的唯一对象 ID,则可以使用distinct关键字。:

select distinct object_id from table_name where status_id in (1, 2)

推荐阅读