首页 > 解决方案 > 搜索过滤列

问题描述

使用 Postgres,我需要在一个有 9 列的表上进行搜索,我只需要其中的 4 个,第一、第二、第六和第七。

我需要的是只有这四列的搜索结果,其中第 6 列和第 7 列不同,在结果中保持第 1、第 2、第 6 和第 7 列的顺序这是第 6 和第 7 列是子项和奖金的示例

我对此很陌生,如果你能告诉我如何得到这个结果,我真的很感激。

psql=> select employee_id, registration, children, bonus from rewards_plan;
    employee_id   |  registration  | children | bonus  
--------------------+--------+----------+--------------
        65000 | 180047     |        1 |       1
        76000 | 154177     |        1 |       0
        97000 | 223181     |        2 |       1
        16000 | 195381     |        1 |       0
        25000 | 301554     |        1 |       1

标签: sqlpostgresql

解决方案


WHERE 子句不限于 SELECT 列表中的列。

您可以使用:

select employee_id, registration, children, bonus 
from rewards_plan
where sixth_column <> seventh_column;

如果这两列可以包含 NULL 值,您可能需要使用“null-safe”比较:

select employee_id, registration, children, bonus 
from rewards_plan
where sixth_column is distinct from seventh_column;

推荐阅读