我有一个非常简单的查询:
SELECT users.id, COUNT(others.id) as c
FROM users JOIN other ON users.id=others.user_id
GROUP BY users.id
我想做的是当c=0
时用''
替换c
中的数字。
例如,而不是一行
# I have this
1, 0
2, 1
# I want c=0 replaced with ''
1, ''
2, 1
我如何在 MySQL 中执行此操作?
最佳答案
试试这个:
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
关于mysql - 根据计数结果选择不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9788735/