MySQL: select 具有相同列名的连接表中的非空值
MySQL: select the not-empty value from joined tables with the same column name
在我的数据库结构中,我有两个 table,其中一些列包含相同的名称。
对于这个例子,我有 table products
和 products_options
。两个 table 都有 sku
.
列
可能在两个 table 或 table 之一中都填充了该列。
我想知道是否可以将查询的 SELECT
部分添加到 select SKU,如下所示:
- 如果
sku
在 products_options
中为空且在 products
中不为空:return products
的值
- 如果
sku
在 products_options
中不为空且在 products
中不为空:return products_options
的值
- 如果
sku
在 products_options
中不为空且在 products
中为空:return products_options
的值
在 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
)
在我的数据库结构中,我有两个 table,其中一些列包含相同的名称。
对于这个例子,我有 table products
和 products_options
。两个 table 都有 sku
.
可能在两个 table 或 table 之一中都填充了该列。
我想知道是否可以将查询的 SELECT
部分添加到 select SKU,如下所示:
- 如果
sku
在products_options
中为空且在products
中不为空:returnproducts
的值
- 如果
sku
在products_options
中不为空且在products
中不为空:returnproducts_options
的值
- 如果
sku
在products_options
中不为空且在products
中为空:returnproducts_options
的值
在 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
)