MySQL: select 具有相同列名的连接表中的非空值

MySQL: select the not-empty value from joined tables with the same column name

在我的数据库结构中,我有两个 table,其中一些列包含相同的名称。 对于这个例子,我有 table productsproducts_options。两个 table 都有 sku.

可能在两个 table 或 table 之一中都填充了该列。 我想知道是否可以将查询的 SELECT 部分添加到 select SKU,如下所示:

在 MySQL 中有这样的可能吗?

您可以像这样尝试 coalesce() 功能:

select coalesce(p1.sku,p2.sku) as sku
from(
    (select id,sku from products_option) as p1
    inner join
    (select id,sku from products) as p2
    on p1.id = p2.id
)