组队学习-sql-20220622

本文详细展示了四个SQL查询作业,涉及数据筛选、合并、子查询和连接操作,旨在找出售价超过500的产品,产品类型中最高价格的商品,以及未在两个表中同时出现的产品。这些查询深入探讨了数据库的复杂查询技巧和数据关联。
摘要由CSDN通过智能技术生成

作业4.1

select *
from product
where sale_price > 500

union

select *
from product2
where sale_price > 500;

在这里插入图片描述

作业4.2

select *
from(
    select *
    from product
    union 
    select *
    from product2
)t
where product_id not in (

select product_id
from product
where product_id not in (select product_id
                           from product2)

union

select product_id
from product2
where product_id not in (select product_id
                           from product)
);

在这里插入图片描述

4.3

select t2.shop_id,t1.product_id,t1.product_type,t1.sale_price
from(
select t2.product_id,t1.product_type,t2.sale_price
from(
select product_type,max(sale_price) as max_price_type
from(
    select *
    from product
    union
    select *
    from product2)t
group by product_type)t1
left join(select *
    from product
    union
    select *
    from product2)t2
on t1.product_type = t2.product_type
and t1.max_price_type = t2.sale_price)t1
left join shopproduct t2
on t1.product_id = t2.product_id
-- 一个嵌套了好多好多层的sql~~

在这里插入图片描述

作业4.4

-- 内连接
select b.product_id
      ,a.product_type
      ,b.sale_price
from(
    select product_type,max(sale_price) as max_price_type
    from(
        select *
        from product
        union
        select *
        from product2
        )t1
    group by product_type
)a
inner join(select *
            from product
            union
            select *
            from product2 
            )b
on a.product_type = b.product_type
and a.max_price_type = b.sale_price

-- 关联子查询
SELECT product_type, product_id, sale_price
  FROM (
    select *
    from product
    union
    select *
    from product2
    ) P1
 WHERE sale_price = (SELECT max(sale_price)
                       FROM (
                                select *
                                from product
                                union
                                select *
                                from product2
                            )p2
                      WHERE P1.product_type = P2.product_type
                      GROUP BY product_type);

在这里插入图片描述

作业4.5

TAT想不出来。。明天继续想

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值