图书--数据库查询

use pubs
--1.	对pubs数据库中的authors表,查询所有作者的信息。
select *from authors
--2.	查询作者所在的城市。
select distinct city from authors
--3.	查询作者编号、姓氏、电话号码及所在城市,查询结果按照作者编号的降序排列。
select au_id,au_fname+' '+au_lname 姓名,phone,city 
from authors
order by au_id desc

--4.	查询作者编号、姓氏、电话号码及所在城市,查询结果按照城市降序排列,同一城市的按作者编号的升序排列。
select au_id,au_fname+' '+au_lname 姓名,phone,city 
from authors
order by city,au_id
--5.	查询居住在Oakland的作者姓名和电话号码。
select au_fname+' '+au_lname 姓名,phone 
from authors
where city in ('Oakland')

select au_fname+' '+au_lname 姓名,phone 
from authors
where city='Oakland'

--6.	查询居住在Oakland或Berkeley的作者姓名和电话号码。查询结果按照城市升序排列。

select au_fname+' '+au_lname 姓名,phone
from authors
where city in ('Oakland','Berkeley')
order by city


--7.	查询所有作者全名,结果先按姓氏升序排列,如有雷同,再按照名字排列。

select au_fname+' '+au_lname 姓名
from authors
order by au_lname,au_fname


--8.	pubs数据库,employee表,查询工资水平在100至172之间的员工编号及工资水平。

select * from employee

select emp_id,job_lvl 
from employee
where job_lvl between 100 and 172




--9.	查询authors表中的姓氏,并返回作者名字的首字母

--答案

select au_lname,substring(au_fname,1,1)--从以一个开始截取第一个
from authors
order by au_lname

--函数用法
SUBSTR(str,pos): 由<str>中,选出所有从第<pos>位置开始的字元。请注意,这个语法不适用于SQL Server上。 
SUBSTR(str,pos,len): 由<str>中的第<pos>位置开始,选出接下去的<len>个字元
SELECT SUBSTR(store_name, 3) 
FROM Geography 
WHERE store_name = 'Los Angeles'; 

结果: 

's Angeles' 

例2: 

SELECT SUBSTR(store_name,2,4) 
FROM Geography 
WHERE store_name = 'San Diego'; 
结果: 
'an D' 
--10.	查询authors表中作者姓氏的第2个字母是‘r’的作者信息
select *from authors
where substring(au_lname,2,1)='r'

--11.	查询authors表中作者姓氏的最后一个字母是‘e’的作者信息.
select *from authors
where au_lname like '%e'


 select *
 from authors
 where right(au_lname,1)='e'--从右数最后一个字母是


--12.	对上面两题,如果执行查询时不知道表中记录的大小写,该怎么办?

select *from authors
where substring(au_lname,2,1) in ('r','R')

select *from authors
where 
au_lname like '%e_'
and
au_lname like '%E_'

--13.	查询pubs数据库employee中每个雇员的工龄。

select * from employee

--错误:select fname+' '+lname 姓名,getdate()-hire_date 工龄 from employee
--改正
select datediff(year,hire_date,getdate())--后面的减去前面的
from employee


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早日退休过上不劳而获生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值