leetcode 1795 sql表的拆分,行转列

博客探讨了在解决LeetCode 1795题时,如何使用SQL进行表格拆分和行转列的巧妙方法。作者通过实例分享了遇到的问题和解决方案,强调了在SQL中使用`IS NOT NULL`而非`!= NULL`的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
此题一眼看去完全不会,经题解指点,发现可以讲常数值直接定位列的值,结合union可以非常巧妙额解决,于是我就画了个葫芦。

select
    product_id, 'store1' as store, store1 as price
from
    products
where
    store1 != null

然后离谱的事就发生了,tm的就不对啊。然后我就研究了一会,发现原来sql是不能!=null的,一定要is not null才行。

# Write your MySQL query statement below
select
    product_id, 'store1' as store, store1 as price
from
    products
where
    store1 is not null

union

select
    product_id, 'store2' as store, store2 as price
from
    products
where
    store2 is not null

union

select
    product_id, 'store3' as store, store3 as price
from
    products
where
    store3 is not null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值