精妙SQL语句介绍
精妙SQL语句介绍,说明:查询从第30条到第40条的记录数 SQL:select top 10 * from temp where id not in (select top 30 id from temp order by id asc) order by id asc 说明:复制表(只复制结构,源表名:a 新表名:b) SQL: select * ...
SQL:select top 10 * from temp where id not in (select top 30 id from temp order by id asc) order by id asc
说明:复制表(只复制结构,源表名:a 新表名:b)
SQL: select * into b from a where 1<1
说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
SQL: insert into b(a, b, c) select d,e,f from b;
说明:显示文章、提交人和最后回复时间
SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
说明:外连接查询(表名1:a 表名2:b)
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
说明:日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff(’minute’,f开始时间,getdate())5
说明:两张关联表,删除主表中已经在副表中没有的信息
SQL:
delete from info where not exists ( select * from infobz where info.infid=infobz.infid )
说明:--
SQL:
SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE FROM TABLE1,(SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE FROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND FROM TABLE2 WHERE TO_CHAR(UPD_DATE,’YYYY/MM’) = TO_CHAR(SYSDATE, ’YYYY/MM’)) X, (SELECT NUM, UPD_DATE, STOCK_ONHAND FROM TABLE2 WHERE TO_CHAR(UPD_DATE,’YYYY/MM’) = TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, ’YYYY/MM’) || ’/01’,’YYYY/MM/DD’) - 1, ’YYYY/MM’) ) Y, WHERE X.NUM = Y.NUM (+)AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) < X.STOCK_ONHAND ) B WHERE A.NUM = B.NUM
说明:--
SQL:
select * from studentinfo where not exists(select * from student where studentinfo.id=student.id) and 系名称=’"&strdepartmentname&"’ and 专业名称=’"&strprofessionname&"’ order by 性别,生源地,高考总成绩
说明: 从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)
SQL:
SELECT a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, ’yyyy’) AS telyear, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’01’, a.factration)) AS JAN, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’02’, a.factration)) AS FRI, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’03’, a.factration)) AS MAR, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’04’, a.factration)) AS APR, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’05’, a.factration)) AS MAY, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’06’, a.factration)) AS JUE,SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’07’, a.factration)) AS JUL, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’08’, a.factration)) AS AGU, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’09’, a.factration)) AS SEP, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’10’, a.factration)) AS OCT, SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’11’, a.factration)) AS NOV,SUM(decode(TO_CHAR(a.telfeedate, ’mm’), ’12’, a.factration)) AS DEC FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration FROM TELFEESTAND a, TELFEE b WHERE a.tel = b.telfax) a GROUP BY a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, ’yyyy’)
说明:四表联查问题:
SQL: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....
说明:得到表中最小的未使用的ID号
Sql 多条件查询的一种简单的方法
以后我们做多条件查询,一种是排列结合,另一种是动态拼接SQL
如:我们要有两个条件,一个日期@addDate,一个是@name
第一种写法是
if (@addDate is not null) and (@name < ’’)
select * from table where addDate = @addDate and name = @name
else if (@addDate is not null) and (@name =’’)
select * from table where addDate = @addDate
else if(@addDate is null) and (@name < ’’)
select * from table where and name = @name
else if(@addDate is null) and (@name = ’’)
select * from table
第二种就是动态组成SQL,通过exec来执行,我就不写,
昨天我想到一种办法
select * from table where (addDate = @addDate or @addDate is null) and (name = @name or @name = ’’)
SQL:
SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID FROM Handle WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)
-1、查找员工的编号、姓名、部门和出生日期,如果出生日期为空值, --显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd。 select emp_no ,emp_name ,dept , isnull(convert(char(10),birthday,120),’日期不详’) birthday from employee order by dept --
2、查找与喻自强在同一个单位的员工姓名、性别、部门和职称 select emp_no,emp_name,dept,title from employee where emp_name<’喻自强’ and dept in (select dept from employee where emp_name=’喻自强’)
--3、按部门进行汇总,统计每个部门的总工资 select dept,sum(salary) from employee group by dept
--4、查找商品名称为14寸显示器商品的销售情况, --显示该商品的编号、销售数量、单价和金额 select a.prod_id,qty,unit_price,unit_price*qty totprice from sale_item a,product b where a.prod_id=b.prod_id and prod_name=’14寸显示器’
--5、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额 select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice from sale_item group by prod_id
--6、使用convert函数按客户编号统计每个客户1996年的订单总金额 select cust_id,sum(tot_amt) totprice from sales where convert(char(4),order_date,120)=’1996’ group by cust_id
--7、查找有销售记录的客户编号、名称和订单总额 select a.cust_id,cust_name,sum(tot_amt) totprice from customer a,sales b where a.cust_id=b.cust_id group by a.cust_id,cust_name
--8、查找在1997年中有销售记录的客户编号、名称和订单总额 select a.cust_id,cust_name,sum(tot_amt) totprice from customer a,sales b where a.cust_id=b.cust_id and convert(char(4),order_date,120)=’1997’ group by a.cust_id,cust_name
--9、查找一次销售最大的销售记录 select order_no,cust_id,sale_id,tot_amt from sales where tot_amt= (select max(tot_amt) from sales)
--10、查找至少有3次销售的业务员名单和销售日期 select emp_name,order_date from employee a,sales b where emp_no=sale_id and a.emp_no in (select sale_id from sales group by sale_id having count(*)=3) order by emp_name
--11、用存在量词查找没有订货记录的客户名称 select cust_name from customer a where not exists (select * from sales b where a.cust_id=b.cust_id)
--12、使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额
--订货日期不要显示时间,日期格式为yyyy-mm-dd
--按客户编号排序,同一客户再按订单降序排序输出 select a.cust_id,cust_name,convert(char(10),order_date,120),tot_amt from customer a left outer join sales b on a.cust_id=b.cust_id order by a.cust_id,tot_amt desc
--13、查找16M DRAM的销售情况,要求显示相应的销售员的姓名、
--性别,销售日期、销售数量和金额,其中性别用男、女表示 select emp_name 姓名, 性别= case a.sex when ’m’ then ’男’ when ’f’ then ’女’ else ’未’ end, 销售日期= isnull(convert(char(10),c.order_date,120),’日期不详’), qty 数量, qty*unit_price as 金额 from employee a, sales b, sale_item c,product d where d.prod_name=’16M DRAM’ and d.prod_id=c.prod_id and a.emp_no=b.sale_id and b.order_no=c.order_no
--14、查找每个人的销售记录,要求显示销售员的编号、姓名、性别、
--产品名称、数量、单价、金额和销售日期 select emp_no 编号,emp_name 姓名, 性别= case a.sex when ’m’ then ’男’ when ’f’ then ’女’ else ’未’ end, prod_name 产品名称,销售日期= isnull(convert(char(10),c.order_date,120),’日期不详’), qty 数量, qty*unit_price as 金额 from employee a left outer join sales b on a.emp_no=b.sale_id , sale_item c,product d where d.prod_id=c.prod_id and b.order_no=c.order_no
--15、查找销售金额最大的客户名称和总货款 select cust_name,d.cust_sum from customer a, (select cust_id,cust_sum from (select cust_id, sum(tot_amt) as cust_sum from sales group by cust_id ) b where b.cust_sum = ( select max(cust_sum) from (select cust_id, sum(tot_amt) as cust_sum from sales group by cust_id ) c ) ) d where a.cust_id=d.cust_id
--16、查找销售总额少于1000元的销售员编号、姓名和销售额 select emp_no,emp_name,d.sale_sum from employee a, (select sale_id,sale_sum from (select sale_id, sum(tot_amt) as sale_sum from sales group by sale_id ) b where b.sale_sum <1000 ) d where a.emp_no=d.sale_id
--17、查找至少销售了3种商品的客户编号、客户名称、商品编号、商品名称、数量和金额 select a.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_price from customer a, product b, sales c, sale_item d where a.cust_id=c.cust_id and d.prod_id=b.prod_id and c.order_no=d.order_no and a.cust_id in ( select cust_id from (select cust_id,count(distinct prod_id) prodid from (select cust_id,prod_id from sales e,sale_item f where e.order_no=f.order_no) g group by cust_id having count(distinct prod_id)=3) h )
--18、查找至少与世界技术开发公司销售相同的客户编号、名称和商品编号、商品名称、数量和金额 select a.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_price from customer a, product b, sales c, sale_item d where a.cust_id=c.cust_id and d.prod_id=b.prod_id and c.order_no=d.order_no and not exists (select f.* from customer x ,sales e, sale_item f where cust_name=’世界技术开发公司’ and x.cust_id=e.cust_id and e.order_no=f.order_no and not exists ( select g.* from sale_item g, sales h where g.prod_id = f.prod_id and g.order_no=h.order_no and h.cust_id=a.cust_id) )
19、查找表中所有姓刘的职工的工号,部门,薪水 select emp_no,emp_name,dept,salary from employee where emp_name like ’刘%’
20、查找所有定单金额高于2000的所有客户编号 select cust_id from sales where tot_amt2000
21、统计表中员工的薪水在4000-6000之间的人数 select count(*)as 人数 from employee where salary between 4000 and 6000
22、查询表中的同一部门的职工的平均工资,但只查询"住址"是"上海市"的员工 select avg(salary) avg_sal,dept from employee where addr like ’上海市%’ group by dept
23、将表中住址为上海市的员工住址改为北京市 update employee set addr like ’北京市’ where addr like ’上海市’
24、查找业务部或会计部的女员工的基本信息。 select emp_no,emp_name,dept from employee where sex=’F’and dept in (’业务’,’会计’)
25、显示每种产品的销售金额总和,并依销售金额由大到小输出。 select prod_id ,sum(qty*unit_price) from sale_item group by prod_id order by sum(qty*unit_price) desc
26、选取编号界于’C0001’和’C0004’的客户编号、客户名称、客户地址。 select CUST_ID,cust_name,addr from customer where cust_id between ’C0001’ AND ’C0004’
27、计算出一共销售了几种产品。 select count(distinct prod_id) as ’共销售产品数’ from sale_item 28、将业务部员工的薪水上调3%。 update employee set salary=salary*1.03 where dept=’业务’
29、由employee表中查找出薪水最低的员工信息。 select * from employee where salary= (select min(salary ) from employee )
30、使用join查询客户姓名为客户丙所购货物的客户名称,定单金额,定货日期,电话号码 select a.cust_id,b.tot_amt,b.order_date,a.tel_no from customer a join sales b on a.cust_id=b.cust_id and cust_name like ’客户丙’
31、由sales表中查找出订单金额大于E0013业务员在1996/10/15这天所接每一张订单的金额的所有订单。 select * from sales where tot_amtall (select tot_amt from sales where sale_id=’E0013’and order_date=’1996/10/15’) order by tot_amt
32、计算’P0001’产品的平均销售单价 select avg(unit_price) from sale_item where prod_id=’P0001’
33、找出公司女员工所接的定单 select sale_id,tot_amt from sales where sale_id in (select sale_id from employee where sex=’F’)