mysql select 如果字段行为空,则替换同表中另一个字段的数据

mysql select if field row is blank substitute data from another field on sametable

uid     external
rn345   --
ev456   --  
--      Peter

所以我有上面的 table 并且我想 select uid 但是我还想将 "external" 字段上的值替换为 "uid" 字段如果值为空。 所以基本上,我会得到以下信息:

UID
rn345
ev456
彼得

这可能吗?

您需要使用 coalesce,如果第一个值是 null,则第二个值 returns:

select  coalesce(uid, external)
from    yourTable

编辑

如果 uid 字段而不是 null 字段为空,您将不得不使用 case 语句来代替

select  case when uid is null or uid = '' then external else uid end
from    yourTable