专业网站托管的公司,购物网站毕业设计论文,网页设计师培训有哪些机构,h5收款平台文章目录 子查询IN运算符子查询 VS 连接ALL关键字ANY关键字相关子查询 !EXISTS运算符select子句中的子查询from子句中的子查询 子查询
获取价格大于id为3的货物的商品 用到了内查询#xff0c;获取id为3的商品的单价#xff0c;把结构传给外查询 在where子句中编写子查询获取id为3的商品的单价把结构传给外查询 在where子句中编写子查询也可以在from或select子句中编写。
use sql_store;
select *
from products
where unit_price (select unit_pricefrom productswhere product_id 3)运行结果
练习查询工资大于平均工资的员工
use sql_hr;
select *
from employees
where salary (select avg(salary)from employees)运行结果
IN运算符
in运算符写子查询查询没有被订购过的商品先在子查询中找出被订购过的商品id注意要去重。将这个查询结果作为子查询在外层找product_id不在子查询结果里的数据就是没有被订购过的商品。
use sql_store;
select *
from products
where product_id not in (select distinct product_idfrom order_items
)练习找到没有支付过支票的客户 从invoices中查询去重后的clientid将这个结果作为内查询传给外查询找不在这个内查询结果中的id就是没有支付过支票的顾客
use sql_invoicing;
select *
from clients
where client_id not in (select distinct client_idfrom invoices
)运行结果
子查询 VS 连接
在运行时间差不多的情况下应该选择最易读的查询要注意代码的可读性上一个练习题可以使用外连接进行查询但这样写可读性不好。
select *
from clients
left join invoices using (client_id)
where invoice_id is null练习 找到订购了货物id为3的顾客这道题用 连接查询 思路更清晰可读性更好
-- 用子查询写
use sql_store;
select customer_id, first_name, last_name
from customers
where customer_id in (select customer_idfrom order_itemsjoin orders using (order_id)where product_id 3
)-- 使用连接查询
select distinct customer_id, first_name, last_name
from customers
join orders using (customer_id)
join order_items using (order_id)
where product_id 3ALL关键字
查询大于3号客户的最大发票的所有数据
use sql_invoicing;
select *
from invoices
where invoice_total (select max(invoice_total)from invoiceswhere client_id 3)用all关键字 查询invoice_total比all后查询到的所有数据都大的数据一个一个的跟all后查询到的结果进行比较
select *
from invoices
where invoice_total all(select invoice_totalfrom invoiceswhere client_id 3)返回结果
max写法和all写法可以相互改写两种写法的可读性都较好
ANY关键字
in 和 any是等价的。查询至少有两张发票的客户id 使用count*查到所有的信息根据client_id分组分组后用having进行条件筛选
select client_id, count(*)
from invoices
group by client_id
having count(*) 2把上述查询当子查询把clients中至少有两张发票的客户信息查出来 where子句中可以用in也可以用 anyin 和 any的效果是一样的用哪种都行
-- in
select *
from clients
where client_id in (select client_idfrom invoicesgroup by client_idhaving count(*) 2
)
-- any
select *
from clients
where client_id any (select client_idfrom invoicesgroup by client_idhaving count(*) 2
)相关子查询 !
查询逻辑先到employees表对每个员工e执行这段子查询计算和e同一个部门的员工的平均工资如果这名员工e的工资高于平均工资就会被返回在结果中。依次一条一条的去查询。这种查询成为相关子查询子查询和外查询存在相关性引用了外查询里出现的别名即e使用相关子查询时这段子查询会在主查询每一行的层面执行所以相关子查询经常执行的很慢。
use sql_hr;
select *
from employees e
where salary (select avg(salary)from employeeswhere office_id e.office_id)练习 查询顾客大于自己平均值的数据
use sql_invoicing;
select *
from invoices i
where invoice_total (select avg(invoice_total)from invoiceswhere client_id i.client_id)EXISTS运算符
获取在发票表中有发票的客户 三种写法子查询外连接exists相关子查询 用in先将in后的子查询运行结果返回给where。in后的子查询会生成一个列表返回给where。如果子查询查到的过多会导致列表特别大这样会妨碍最佳性能对于这种情况用exists能提高效率
select *
from clients
where client_id in (select distinct client_idfrom invoices)用exists运算符来查看发票表里是否存在符合这个条件的行子查询并没有给外查询返回一个结果它会返回一个指令说明这个子查询中是否有符合这个搜索条件的行每一行外层查询的数据都到exists后去看是否存在如果存在子查询就会给exists返回trueexists运算符就会在最终结果里添加当前的记录。
select *
from clients
where exists(select client_idfrom invoiceswhere invoices.client_id clients.client_id
);运行结果
练习 找到从没有被订购过的商品
use sql_store;
select *
from products
where not exists(select product_idfrom order_itemswhere order_items.product_id products.product_id
)运行结果
select子句中的子查询
在select子句中用子查询得到平均值select语句中 在表达式中不能使用列的别名这样就只能把select子句中的子查询再复制一遍但是这样很长很麻烦且重复解决方法是再转换成一个子查询select invoice_average
use sql_invoicing;
select invoice_id,invoice_total,(select avg(invoice_total)from invoices) as invoice_avearge,invoice_total - (select invoice_avearge) as difference
from invoices运行结果
练习得到每个客户的总发票金额全部发票的平均值以及他们的差值
select client_id,name,(select sum(invoice_total)from invoiceswhere client_id c.client_id) as total_sales,(select avg(invoice_total)from invoices) as average,(select total_sales) - (select average) as difference
from clients c运行结果
from子句中的子查询
可以把一段查询生成的表当作另一个查询的from
select *
from(
select client_id,name,(select sum(invoice_total)from invoiceswhere client_id c.client_id) as total_sales,(select avg(invoice_total)from invoices) as average,(select total_sales) - (select average) as difference
from clients c
) as hahah
where total_sales is not null运行结果