子查询

简介: 子查询

子查询与SQL语句分析

关联(correlated)/非关联(uncorrelated)
等值/非等值


from 使用子查询作为数据源

/查询用户名和他们所做的“现金交易”的总额/

select username as 'user name', 
    (case when cashtransactions.totalcash is null then 0
             when cashtransactions.totalcash is  not null then totalcash end) as 'total cash'
from users
left join (select userid, sum(transactionamount) as 'totalcash' from transactions where transactiontype = 'cash' group by userid ) as cashtransactions
on users.userid = cashtransactions.userid
order by users.userid; 

改进与优化
/用函数isnull()代替case/

select username as 'user name', 
       isnull(cashtransactions.totalcash, 0) as 'total cash'
from users
left join (select userid, sum(transactionamount) as 'totalcash' from transactions where transactiontype = 'cash' group by userid ) as cashtransactions
on users.userid = cashtransactions.userid
order by users.userid;   

/标准化语句格式/

select u.username as 'user name', 
       (case when cashtransactions.totalcash is null then 0
          when cashtransactions.totalcash is not null then totalcash end) as 'total cash'
from users u
left join 
    (select userid, sum(transactionamount) as 'totalcash' from transactions where transactiontype = 
    'cash' group by userid) as cashtransactions
on u.userid = cashtransactions.userid;

/公用表表达式/

with cashtransactions as
    (select userid, sum(transactionamount) as 'totalcash' from transactions where transactiontype = 'cash' group by userid )
select username as 'user name', 
      (case when cashtransactions.totalcash is null     then 0
            when cashtransactions.totalcash is not null then totalcash end) as 'total cash'
from users
left join cashtransactions
on users.userid = cashtransactions.userid
order by users.userid;  

select 使用子查询作为一个计算列

/制作表用户以及他们所做的交易的数量/

select username as 'user name', 
       (select count(transactionid) from transactions where users.userid = transactions.userid)             
          as 'number of transactions'
from users 
order by users.userid;

子查询改为连接查询

select u.username as 'user name', 
       count(transactionid) as 'number of transactions'
from users u
left join transactions t
on u.userid = t.userid
group by u.userid, u.username
order by u.userid;

where 在查询条件中使用子查询

1.in
/使用现金交易的用户/

select username as 'user name'
from users
where userid in
        (select userid from transactions where transactiontype = 'cash'); -- 标量

子查询改为连接查询

select u.username as 'user name'
from users u
inner join transactions t
on u.userid = t.userid
where t.transactiontype = 'cash'
group by u.username;  -- 确保每位用户只返回一行记录

2.exists
/查找哪一位用户做了交易/

select username as 'user name'
from users
where exists (select * from transactions where users.userid = transactions.userid);  -- 关联子查询

子查询改为连接查询

select username as 'user name' 
from users
where userid in (select userid from transactions);

select username as 'user name' 
from users u
inner join transactions t
on u.userid = t.userid
group by username;

3.关联子查询
/交易总额少于20美元的用户列表/

select username as 'user name'
from users
where (select sum(transactionamount) from transactions where users.userid = transactions.userid) < 20 ;

子查询改为连接查询

select u.username as 'user name'
from users u
left join transactions t
on u.userid = t.userid
group by u.userid, u.username          -- 关联
having sum(transactionamount) < 20;
目录
相关文章
|
6月前
|
SQL 关系型数据库 MySQL
MySQL数据库子查询练习——单个数据的子查询
MySQL数据库子查询练习——单个数据的子查询
36 1
|
7月前
(头哥)多表查询与子查询
(头哥)多表查询与子查询
147 0
|
7月前
|
SQL
深入了解关联查询和子查询
深入了解关联查询和子查询
76 0
|
7月前
聚合函数、子查询
聚合函数、子查询
|
SQL 关系型数据库 MySQL
第9章_子查询
第9章_子查询
72 0
|
SQL 索引
相关子查询
相关子查询
241 0
|
SQL 关系型数据库 MySQL
第09章_子查询
第09章_子查询
104 0
联合查询和子查询
联合查询和子查询
|
SQL 关系型数据库 MySQL
子查询(1)
子查询指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从MySQL 4.1开始引入。
子查询(1)
|
数据库管理
子查询(2)
如果子查询的执行依赖于外部查询,通常情况下都是因为子查询中的表用到了外部的表,并进行了条件关联,因此每执行一次外部查询,子查询都要重新计算一次,这样的子查询就称之为 关联子查询 。相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询。
子查询(2)