资源描述:
《《精妙sql语句》word版》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、精妙SQL语句说明:复制表(只复制结构,源表名:a新表名:b) SQL:select*intobfromawhere1<>1 说明:拷贝表(拷贝数据,源表名:a目标表名:b) SQL:insertintob(a,b,c)selectd,e,ffromb; 说明:显示文章、提交人和最后回复时间 SQL:selecta.title,a.username,b.adddatefromtablea,(selectmax(adddate)adddatefromtablewheretable.title=a.title)b 说明:外连接查询(表名1:a表名2:b) SQL
2、:selecta.a,a.b,a.c,b.c,b.d,b.ffromaLEFTOUTJOINbONa.a=b.c 说明:日程安排提前五分钟提醒 SQL:select*from日程安排wheredatediff('minute',f开始时间,getdate())>5 说明:两张关联表,删除主表中已经在副表中没有的信息 SQL:deletefrominfowherenotexists(select*frominfobzwhereinfo.infid=infobz.infid) 说明:-- SQL: SELECTA.NUM,A.NAME,B.UPD_DATE,B.PR
3、EV_UPD_DATE FROMTABLE1, (SELECTX.NUM,X.UPD_DATE,Y.UPD_DATEPREV_UPD_DATE FROM(SELECTNUM,UPD_DATE,INBOUND_QTY,STOCK_ONHAND FROMTABLE2 WHERETO_CHAR(UPD_DATE,'YYYY/MM')=TO_CHAR(SYSDATE,'YYYY/MM'))X, (SELECTNUM,UPD_DATE,STOCK_ONHAND FROMTABLE2 WHERETO_CHAR(UPD_DATE,'YYYY/MM')= TO_CHAR(T
4、O_DATE(TO_CHAR(SYSDATE,'YYYY/MM')¦¦'/01','YYYY/MM/DD')-1,'YYYY/MM'))Y, WHEREX.NUM=Y.NUM(+) ANDX.INBOUND_QTY+NVL(Y.STOCK_ONHAND,0)<>X.STOCK_ONHAND)B WHEREA.NUM=B.NUM 说明:-- SQL: select*fromstudentinfowherenotexists(select*fromstudentwherestudentinfo.id=student.id)and系名称='"&strdepartment
5、name&"'and专业名称='"&strprofessionname&"'orderby性别,生源地,高考总成绩 说明:从数据库中去一年的各单位电话费统计(电话费定额和电话费清单两个表来源) SQL: SELECTa.userper,a.tel,a.standfee,TO_CHAR(a.telfeedate,'yyyy')AStelyear, SUM(decode(TO_CHAR(a.telfeedate,'mm'),'01',a.factration))ASJAN, SUM(decode(TO_CHAR(a.telfeedate,'mm'),'02',a.fac
6、tration))ASFRI, SUM(decode(TO_CHAR(a.telfeedate,'mm'),'03',a.factration))ASMAR, SUM(decode(TO_CHAR(a.telfeedate,'mm'),'04',a.factration))ASAPR, SUM(decode(TO_CHAR(a.telfeedate,'mm'),'05',a.factration))ASMAY, SUM(decode(TO_CHAR(a.telfeedate,'mm'),'06',a.factration))ASJUE, SUM(decode(TO_
7、CHAR(a.telfeedate,'mm'),'07',a.factration))ASJUL, SUM(decode(TO_CHAR(a.telfeedate,'mm'),'08',a.factration))ASAGU, SUM(decode(TO_CHAR(a.telfeedate,'mm'),'09',a.factration))ASSEP, SUM(decode(TO_CHAR(a.telfeedate,'mm'),'10',a.factration))ASOCT, SUM(deco