Users
+----+------+
| id | name |
+----+------+
| 1 | John |
+----+------+
| 2 | Jane |
+----+------+
Message
+----+------+--------------+
|u_id| date | msg |
+----+------+--------------+
| 1 | 0945 | some text1 |
+----+------+--------------+
| 0 | 0950 | some Text2 |
+----+------+--------------+
| 2 | 1045 | some Text3 |
+----+------+--------------+
我希望将“ not_registered_user”文本用作用户名,其中用户ID为0(对于非注册用户,每次为0)。所需的输出将是:
+------+--------------+--------------+
| date | name | msg |
+------+--------------+--------------+
| 0945 | John | some text1 |
+------+--------------+--------------+
| 1045 | Jane | some text3 |
+------+--------------+--------------+
| 0950 | non_reg_user | some text2 |
+------+--------------+--------------+
请帮助,谢谢。
答案 0 :(得分:2)
使用left join
和coalesce()
select date,coalesce(name,'non_reg_user') as name,msg
from message a
left join users b on a.u_id=b.id