我有两个表,一个包含用户数据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
是非空值,则为true;如果为空,则返回false)。有什么指针吗?
答案 0 :(得分:5)
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
将返回0
表示false,1
表示true。但是,如果您想要true
或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