----题5:求出住在同一城市的顾客对
--select city,count(cid) as 顾客的个数

from customers group by city
--select
--select distinct

c1.cname,c2.cname,c1.city from

customers c1, customers c2 where

c1.city=c2.city and c1.cname<c2.cname

select c1.cid,c2.cid from customers

c1,customers c2
where c1.city=c2.city and

c1.cid<c2.cid
--此题的关键点即为可以给一个表取两个别

名.如果题目要求在一个表中的同一列取出

匹配项的话都可以用这种方法,如果一个城

市有三个顾客呢?

2、in和exists的问题

--题6:检索由住在Duluth的顾客和住在New York 的代理商组成的所有订货记录的ordno值
select ordno from orders where cid in (select cid from customers where city='Duluth') and aid in (select aid from agents where city='New York') --6ms
--答案:
select ordno from orders x where exists (select cid,aid from customers c,agents a
where c.cid=x.cid and a.aid=x.aid and c.city='Duluth' and a.city='New York')  --10ms
--疑惑:难道in比exists执行效率高,还是只是该题的问题

--题7:找出佣金百分率最小的代理商的aid值
select top(1) aid from agents order by [percent]  --我能想到的就是排序然后取第一个,但是我这样做有问题,因为我求出来的只可能有 一个,而实际情况是可能有相同值的不止一个
--答案:
select  aid from agents where [percent]<=all(select [percent] from agents)

----题8:找出住在Dallas或Boston的顾客拥有相同折扣的所有顾客
--select  c1.cname ,c2.cname from customers c1,customers c2 where c1.discnt=c2.discnt and c1.cid<c2.cid --该方法得出的结果跟实际不符合
----我没想出来,该怎么做?
--题9:找出与住在Dallas或Boston的顾客拥有相同折扣的所有顾客
select cid,cname from customers where discnt in (select discnt from customers where city='Dallas' or city='Boston')
--答案:
select cid,cname from customers where discnt=some(select discnt from customers where city='Dallas' or city='Boston')
--执行效率:in 3ms,some 6ms,难道in 的执行效率比some高?

--题10:求出所有满足一下条件的顾客的cid值:该顾客的discnt值小于任一住在Duluth的顾客的discnt值
select cid from customers where discnt<any(select discnt from customers where city='Duluth') --这里是错误的,题目中的任一应该是对应所有的,所以应把any改为all
--这种题目应谨慎,留意

题12:求出既订购了产品p01又订购了产品p07的顾客的cid值

--正确答案:
select distinct cid from  orders x
where pid='p01' and exists (select * from orders where cid=x.cid and pid='p07')
--为什么这里一定要取别名
--取别名除了有方便的好处外,有什么情况是必须用到的吗?

select cid from orders where pid='p01' intersect select cid from orders where pid='p07'
--注:两个的交集,可以用intersect关键字

--3.4.12 检索没有通过代理商a05订货的所有顾客的名字
select cid,cname from customers where cid not in (select cid from orders where aid='a05')
--这个时候in 不能用exists 代替

--3.5.4 找出订购了产品p01和价格超过1美元的所有产品的代理商的aid值
select aid from orders where dollars/qty>1 intersect select aid from orders where pid='p01'  --并且或交集的意思在SQL里面如何表达?
--
select aid from orders where pid in (select pid from products where price>1 or pid='p01' )
--这显然也是错误的,不是要它满足某个条件就行,而是要同时包含这两者
--此题没想出来
--可见,求交集的时候intersect的重要性

--答案:
select y.aid from orders y where y.pid='p01' and not exists (select p.pid from products p where p.price>1.0000 and
not exists (select * from orders x where x.pid=p.pid and x.aid=y.aid))

--3.5.5 找出具有以下性质的顾客的cid 值:如果顾客c006订购了某种产品,那要检索的顾客也订购了该产品
select cname,cid from customers where cid in (select cid from orders where pid in (select pid from orders where cid='c006'))
--跟答案不符,那么该怎么写呢?问题还是应该为包含,而不是在其中满足某个条件
--答案:
select cid from customers c where not exists (select z.pid from orders z
 where z.cid='c006' and not exists (select * from orders y where y.pid=z.pid and y.cid=c.cid) 
)

--3.5.6 找出被所有住在Duluth的顾客订购的产品的pid值
select distinct pid from orders where cid in (select cid from customers where city='Duluth' )
--同理:肯定是错的,对待这种要包含的问题该如何写sql语句
--答案:
select pid from products p where not exists (select c.cid from customers c where c.city='Duluth'
and not exists (select * from orders x where x.pid=p.pid and x.cid=c.cid)
)

--3.6.2 检索没有通过代理商a05订货的所有顾客的名字
select cname from customers except
(select cname from customers,orders where customers.cid=orders.cid and orders.aid='a05')
--这时except是关键

--3.6.4 检索至少订购了一件价格低于¥0.50 的商品的所有顾客的姓名
--答案:我没做出来,下面这种方法运行没通过
select distinct cname from (orders join products using(pid)) join customers using(cid) where price<0.50

--法2:将3个表直接连接起来就可以了
select distinct cname from (orders o join products p on o.pid=p.pid) join customers c on o.cid=c.cid where p.price<0.5

--3.8.2 打印出代理商的名字和标识号、产品的名字和标识号以及每个代理商为顾客c002和c003订购该产品的总量
select aname,aid,pname,pid,sum(qty) as total
select aid,pid,sum(qty) as total from orders where cid='c002' or cid=
select aid,cid,pid,sum(qty) as total from orders where cid in ('c002','c003') group by aid,cid,pid inner
select aname,orders.aid,pname,orders.pid,sum(qty) as total from orders inner join
  agents on agents.aid=orders.aid inner join products on orders.pid=products.pid where
 cid in ('c002','c003') group by orders.aid,orders.cid,orders.pid

--答案:
select aname,a.aid,pname,p.pid,sum(qty) as 每个代理商为每个顾客订购的该产品的总量 from orders x,products p,agents a
where x.pid=p.pid and x.aid=a.aid and x.cid in('c002','c003')
group by a.aid,aname,p.pid,p.pname

--3.8.5 构造一个查询来求出所有代理商的最大销售额平均值
select avg(select max(dollars)
from orders group by aid) from orders
--该SQL语句之所以有错误是因为基本sql不允许集合函数内部包含子查询也不允许from子句包含子查询
--但如果使用扩展语法,我们就能将子查询放在from子句中,通过对表重新命名并对集合函数生成的列命名,我们就能如愿以偿地实现上述查询
select avg(t.x) from (select aid,max(dollars) as x from orders group by aid) t
--由此可看出取别名的好处允许from子句包含子查询

--3.9.4 检索cname值以字母‘A’打头的顾客的所有信息
select * from customers where cname like 'A%'--注意:相似查询这样用

--3.9.5 检索cname值的第三个字母不等于“%”的顾客的cid值
--不知道怎么做?
select cid from customers where cname not like '__\%%' escape'\'

--3.9.6 检索cname值以'Tip_’打头并且后面跟着任意个字符的顾客的cid值
select cid,cname from customers where cname like 'Tip_%'
--答案:
select cid from customers where cname like 'Tip\_%' escape '\' --难道这种方法有错误 ?得不出答案,上面的刚好可以满足题意,但是其他题目又可以

转载于:https://www.cnblogs.com/chenwancoco/archive/2011/08/12/2135990.html

sql中的遇到的有问题的相关推荐

  1. SQL 中循环、for循环、游标

    我们使用SQL语句处理数据时,可能会碰到一些需要循环遍历某个表并对其进行相应的操作(添加.修改.删除),这时我们就需要用到咱们在编程中常常用的for或foreach,但是在SQL中写循环往往显得那么吃 ...

  2. SQL中的case when then else end用法

    2019独角兽企业重金招聘Python工程师标准>>> Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sexWHEN '1' THEN ...

  3. 在SQL中使用CRL函数示例

    在SQL中使用CRL函数 实验目标: 1. 在SQL中创建CRL函数,使之能够向指定的计算机发送消息 实验步骤 2. 在VS中创建类发送消息的类 3. 将以下代码黏贴进去 using System; ...

  4. pl/sql中三种游标循环效率对比

    pl/sql中三种游标循环效率对比 - Oracle数据库栏目 - 红黑联盟 http://www.2cto.com/database/201307/224636.html 转载于:https://b ...

  5. SQL中内连接、外连接、交叉连接

    SQL中内连接.外连接.交叉连接 SQL连接可以分为内连接.外连接.交叉连接. 数据库数据:            book表                                      ...

  6. 简单介绍SQL中ISNULL函数使用方法

    这篇文章介绍了SQL Server.MySQL.Oracle三种数据库中ISNULL函数的使用方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 SQL Ser ...

  7. SQL 中left join ,inner join,right join的区别

    2019独角兽企业重金招聘Python工程师标准>>> sql中存在有左连接  插入 右连接 sql语句 left join ,inner join,right join 进行 wh ...

  8. 什么是存储过程?什么是触发器?SQL中存储过程与触发器的区别是什么?

    什么是存储过程?什么是触发器?SQL中存储过程与触发器的区别是什么? 存储过程是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,它存储在数据库中,一次编译后永久有效,用户通过指定存储过程的名 ...

  9. SQL中where与having的区别

    SQL中where与having的区别 目录 SQL中where与having的区别 创建表 插入数据 进行聚合操作 # WHERE是在聚合之前进行数据行的过滤而HAVING实在聚合之后进行新数据的过 ...

  10. php exec和query,关于Go SQL中的Query、Exec和Prepare使用对比(附网络抓包)

    下面由golang教程栏目给大家介绍关于Go SQL中的Query.Exec和Prepare使用对比(附网络抓包) ,希望对需要的朋友有所帮助! Go 附带的 database/sql 库可以与任何关 ...

最新文章

  1. LeetCode简单题之判断路径是否相交
  2. .net程序员面试题
  3. 了解下RDF 主要元素
  4. 分享几个 SpringBoot 实用的小技巧
  5. C#中调用Windows API的要点【转载】
  6. Response内置对象
  7. IT项目管理不能犯的错误
  8. 面向对象程序设计第二次作业(2)
  9. selenium自动化测试_49自动化测试中最常见的Selenium异常
  10. “父母双学霸, 生娃是学渣”的科学解释是什么?
  11. mysql表空间界限_MySQL5.7 import表结构报错超出表空间界限
  12. JSP页面处理和输出
  13. c语言plc编程软件下载,罗克韦尔plc编程软件下载-2017罗克韦尔plc编程软件V21.00 官方最新版 - 极光下载站...
  14. 2440/6410+minitools+superboot烧写裸机新方法!
  15. pid控制车辆沿着圆弧轨迹跟踪
  16. 【水】java试手—poj 2387
  17. keil5打开工程文件显示空白bug
  18. uefi模式安装windows10和deepin
  19. 软件测试mysql面试题:Rename和Alias有什么区别?
  20. 洛谷1710 地铁涨价

热门文章

  1. 中点和中值滤波的区别_频谱仪和EMI测试接收机什么区别?安泰维修中心分享
  2. C 运算符中不能重载的是哪些
  3. Python3最长连续递增序列问题
  4. 计算机语言 angela,Angela
  5. ubuntu安装Qt开发环境
  6. Ubuntu16U盘没有不能新建粘贴文件[sdf] Write cache: disabled, read cache: enabled, doesn‘t support DPO or FUA
  7. pat 乙级 1010 一元多项式求导(C++)
  8. 光纤以太网交换机产品性能介绍
  9. 工业交换机与工业路由器的区别
  10. 关于单纤与双纤光端机的区别介绍