左外连接 - 如何在第二个表中返回存在的布尔值?

Ale*_*ber 10 postgresql join coalesce left-join outer-join

在CentOS 6上的PostgreSQL 9中,pref_users表中有60000条记录:

# \d pref_users
                   Table "public.pref_users"
   Column   |            Type             |     Modifiers      
------------+-----------------------------+--------------------
 id         | character varying(32)       | not null
 first_name | character varying(64)       | not null
 last_name  | character varying(64)       | 
 login      | timestamp without time zone | default now()
 last_ip    | inet                        | 
 (... more columns skipped...)
Run Code Online (Sandbox Code Playgroud)

另一张桌子可容纳大约500名用户,不允许再玩游戏:

# \d pref_ban2
                 Table "public.pref_ban2"
   Column   |            Type             |   Modifiers   
------------+-----------------------------+---------------
 id         | character varying(32)       | not null
 first_name | character varying(64)       | 
 last_name  | character varying(64)       | 
 city       | character varying(64)       | 
 last_ip    | inet                        | 
 reason     | character varying(128)      | 
 created    | timestamp without time zone | default now()
Indexes:
    "pref_ban2_pkey" PRIMARY KEY, btree (id)
Run Code Online (Sandbox Code Playgroud)

在PHP脚本中,我试图pref_users在jQuery-dataTable中显示所有60000个用户.我想标记被禁用的用户(找到的用户pref_ban2).

这意味着我需要一个为ban我的查询中的每个记录命名的列truefalse.

所以我正在尝试左外连接查询:

# select                          
       b.id,  -- how to make this column a boolean?
       u.id, 
       u.first_name, 
       u.last_name, 
       u.city,
       u.last_ip,
       to_char(u.login, 'DD.MM.YYYY') as day
from pref_users u left outer join pref_ban2 b on u.id=b.id
limit 10;
 id |    id    | first_name  | last_name |    city     |     last_ip     |    day     
----+----------+-------------+-----------+-------------+-----------------+------------
    | DE1      | Alex        |           | Bochum      | 2.206.0.224     | 21.11.2014
    | DE100032 | ?????? ???? |           | London      | 151.50.61.131   | 01.02.2014
    | DE10011  | A???????? ? |           | ??????????? | 37.57.108.13    | 01.01.2014
    | DE10016  | Semen10     |           | usa         | 69.123.171.15   | 25.06.2014
    | DE10018  | ????????    |           | ????????    | 178.216.97.214  | 25.09.2011
    | DE10019  | -???????-   |           | ?????       | 5.140.81.95     | 21.11.2014
    | DE10047  | ???????     |           | C???        | 95.132.42.185   | 25.07.2014
    | DE10054  | Maedhros    |           | ??????      | 207.246.176.110 | 26.06.2014
    | DE10062  | ssergw      |           | ??????      | 46.188.125.206  | 12.09.2014
    | DE10086  | ?????       |           | ????        | 109.111.26.176  | 26.02.2012
(10 rows)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,b.id上面的列是空的 - 因为这10个用户没有被禁止.

如何false在该列中获取值而不是String?

而且我不是在追求某些coalescecase表达,而是在寻找"正确"的方式来进行这样的查询.

Jor*_*mer 11

具有外连接的CASE或COALESCE语句是执行此操作的正确方法.

select
  CASE 
    WHEN b.id IS NULL THEN true
    ELSE false
  END AS banned,                          
  u.id, 
  u.first_name, 
  u.last_name, 
  u.city,
  u.last_ip,
  to_char(u.login, 'DD.MM.YYYY') as day
from pref_users u 
left outer join pref_ban2 b 
  on u.id=b.id
limit 10;
Run Code Online (Sandbox Code Playgroud)

  • 我在这里误解了什么吗?但是“IS NULL”和“IS NOT NULL”已经返回布尔值,那么 CASE WHEN 真的需要吗?难道你不能只选择列:“b.id IS NOT NULL as ban” (2认同)

LaV*_*che 6

"IS NULL"和"IS NOT NULL"返回一个布尔值,因此这应该很容易.

我想这就是你需要的一切吗?

SELECT                          
       b.id IS NOT NULL as is_banned, -- The value of "is_banned" will be a boolean
Run Code Online (Sandbox Code Playgroud)

不确定你是否需要"不",但你会得到一个bool.