我有一个非常简单的查询:
SELECT users.id, COUNT(others.id) as c
FROM users JOIN other ON users.id=others.user_id
GROUP BY users.id
我想要做的是在c
时用''
替换c=0
中的号码。
例如,而不是像
这样的行 # I have this
1, 0
2, 1
# I want c=0 replaced with ''
1, ''
2, 1
我如何在MySQL中执行此操作?
答案 0 :(得分:3)
试试这个:
SELECT users.id, IF(COUNT(others.id) = 0, '', COUNT(others.id)) as c
FROM users JOIN other ON users.id=others.user_id
GROUP BY users.id
答案 1 :(得分:3)
SELECT t.id, CASE t.c = 0 THEN '' ELSE t.c FROM (
SELECT users.id as id, COUNT(others.id) as c
FROM users JOIN other ON users.id=others.user_id
GROUP BY users.id
) as t
答案 2 :(得分:0)
你可以试试这个:
SELECT users.id, IF(COUNT(others.id) > 0, COUNT(others.id), "''") as c
FROM users JOIN other ON users.id=others.user_id
GROUP BY users.id
或者有COALESCE但是如果不能使用0结果
“''”(2个双引号中的2个单引号)会给你
1,''而不是
1,