sql语句_2
数据表如下
一个user_id对应多个user_name,现在要求是:如果某个用户对应的user_name中存在一个a,打印user_id,a出来;如果不存在,打印user_id,0。打印时候user_id不重复。
SELECT * FROM ( (SELECT DISTINCT user_id, user_name FROM user1 WHERE user_name = 'a') UNION ALL (SELECT DISTINCT user_id,'0' AS user_name FROM user1 WHERE user_name <> 'a' AND user_id NOT IN (SELECT DISTINCT user_id FROM user1 WHERE user_name = 'a') ) ) AS k
输出结果: