浅谈select语句

这里写图片描述


创建三个表

create table _user (
            ID int CONSTRAINT pk_user primary key,
            Name varchar(10) 
            )
create table product (
            ID int CONSTRAINT pk_product primary key,
            Name varchar(10), 
            price int,
            comment varchar(10)
            )
create table subscribe  (
            ID int primary key,         
            _user_id int CONSTRAINT fk_subscribe_user FOREIGN KEY references _user(ID)
                            ON UPDATE CASCADE
                            ON DELETE CASCADE ,
            product_id int CONSTRAINT fk_subscribe_product FOREIGN KEY references product(ID)
                            ON UPDATE CASCADE
                            ON DELETE CASCADE ,
            _Time int 
            )
insert into _user
 select     1,'刘德华' union all
 select     2,'周润发' union all
 select     3,'张惠妹' ;

insert into _user
 select     1,'刘德华' union all
 select     2,'周润发' union all
 select     3,'张惠妹' ;

insert into product
 select     1,'首映专区',30,'包月' union all
 select     2,'看大片',20,'包月' union all
 select     3,'全能包',199,'包年' ;

insert into subscribe
 select     1,1,1,20155 union all
 select     2,1,2,20156 union all
 select     3,2,3,20158 ;

对应的答案

select u.name,p.name,s._time from
(_user as u right join subscribe as s on u.id = s._user_id) 
left join product as p on s.id = p.id;

select p.name,count(s.product_id)as 次数,
p.price*count(s.product_id) as preice
from subscribe as s right join product as p on s.product_id = p.id 
group by p.name,s.product_id,p.price;

select ID,Name from
(select u.ID,u.Name,s._user_id from  _user as u 
left join subscribe as s on u.ID =s._user_id)as A 
where  _user_id IS NULL;

delete from subscribe where _user_id = 1

这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值