我正在查询table a
每行的名称,但是,如果该行具有bID
值(这将是表b的id的外键),我想替换名称值table a
table b
table a
---------
id / bID (may be null) / name
table b
---------
id / name
SELECT a.id as id, a.name as name, a.bid
FROM `table a` as a
LEFT JOIN `table b` as b ON a.bID = b.id
ORDER BY id ASC
。我怎么能这样做?
{{1}}
答案 0 :(得分:1)
“b”表名称上的简单ISNULL
就足够了。您可以使用COALESCE
代替,如果您需要,也可以使用多个参数,而不仅仅是ISNULL
。
SELECT a.id as id, ISNULL(b.name, a.name) as name, a.bid
FROM `table a` as a
LEFT JOIN `table b` as b ON a.bID = b.id
ORDER BY id ASC