例如,我有 2 张桌子
user_table level_table
================== ===================
| name | levelid | | id | levelname |
================== ===================
| tom | 1 | | 1 | beginner |
------------------ -------------------
| jon | 2 | | 2 | intermed |
------------------ -------------------
| tom | null | | 3 | expert |
------------------ -------------------
| jon | 1 |
------------------
我只需要选择具有级别名称的用户表,如果为空,它将显示我定义的文本,例如“无级别”。并且没有相同的列标题。所以它看起来像左边的而不是右边的。
Right result Wrong result
================== ==================
| name | level | | name | level |
================== ==================
| tom | beginner | | tom | beginner |
------------------ ------------------
| jon | intermed | | jon | beginner |
------------------ ------------------
| tom | no level | | tom | beginner |
------------------ ------------------
| jon | beginner | | jon | beginner |
------------------ ------------------
我试过合并,但似乎有一个错误,所有 levelname 行都显示相同的结果。
SELECT name,
COALESCE(
(SELECT levelname FROM user_table,level_table WHERE levelid=id),
'no level') AS level
FROM user_table