5

我有两张表,一张包含用户数据,user另一张包含那些将他们的帐户与 Twitter 绑定的用户,user_twitter。由于第二个表不包含每个用户的对应值(例如,那些尚未将其帐户与 Twitter 连接的用户),我正在使用 LEFT JOIN 进行查询:

SELECT DISTINCT `user_twitter`.uid AS `connected` 
FROM `user` 
LEFT JOIN (SELECT uid FROM `user_twitter`) AS `user_twitter` ON `user`.uid = `user_twitter`.uid

我想要做的是返回一个布尔值(如果user_twitter.uid是非空值则为真,如果为空则为假)。任何指针?

4

1 回答 1

8
SELECT  a.uid AS `connected`,
        (b.uid IS NOT NULL) bool_val
FROM    `user` a
        LEFT JOIN `user_twitter` b
            ON a.uid = b.uid

将返回0false 和1true。true但是,如果您想要or的字符串值false

SELECT  a.uid AS `connected`,
        CASE WHEN b.uid IS NOT NULL THEN 'True' ELSE 'False' END bool_val
FROM    `user` a
        LEFT JOIN `user_twitter` b
            ON a.uid = b.uid
于 2013-04-05T07:18:56.673 回答