说明:复制表(只复制结构,源表名: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=stude
nt.id) and 系名称='"&strdepartmentname&"' and 专业名称='"&strprofessionname&"' order by 性别,生源地,高考总成绩
说明:从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)
SQL:
SELECT a.userper, a.tel, a.standfee, TO_lfeedate, 'yyyy') AS telyear,
说明:四表联查问题:
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:
SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THE
N MIN(HandleID) + 1 ELSE 1 END) as HandleID FROM Handle WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)
查重复的记录
select * from fish where fish_name in
(select fish_name from
(select fish_name,count(fish_name) from fish
group by fish_name
having count(fish_name)>1));
统计某字段中所有不同的记录的个数
select nsrbm,count(nsrbm)
from t11_nsrjbqk
where ...
group by nsrbm
having count(nsrbm)>1
SELECT *
FROM letters
WHERE ((.ID IN (SELECT ID FROM letters As Tmp GROUP BY ID HAVING Count(*)>1 )))
ORDER BY ID;
在SQL Enterprise Manager裡面查詢Access裡面的數據drop删除表
SELECT *
FROM OPENROWSET('msdasql', 'dsn=odbc數據源名;trusted_connection=yes',
'select * from table')
SQL中isnull函数的作用是将将两个参数字段中不为空的值取出
SELECT t1.a, t1.b, ISNULL(t1.c, t2.d)
FROM t1, t2
WHERE t1.a = t2.a
为查询的结果编上序列号
select IDENTITY(int,1,1)as id,a,b,c into #1 from table
select * from #1
或
SELECT rowno =
(SELECT SUM(row)
FROM (SELECT 1 AS row, *
FROM table) AS a
WHERE a.autoid <= b.autoid), *
FROM table b
ORDER BY autoid
日期型字段的操作
select * from inetsrvlog where
datepart(mm,logtime)= 3
and datepart(dd,logtime)>=1
and datepart(dd,logtime)<=15
insert into tablename (...,..) values ...,...
PL/SQL精典编程例:
說明:用SCOTT/TIGER登入ORACLE,執行看看效果,可以將某個字段中的多行放入同一行,以前以為不可能用一SQL實現在,竟然可以。主要理解lead() over(),row_number() over()就可以啦,很奇妙的ORACLE吧,有誰有更好的辦法或SQL SERVER的實現,也請貼出。
SQL> select deptno, dname, emps
2 from (
3 select d.deptno, d.dname, ame ||', '||
4 ame,1) over (partition by d.deptno
5 order ame) ||', '||
6 ame,2) over (partition by d.deptno
7 order ame) ||', '||
8 ame,3) over (partition by d.deptno
9 order ame) ||', '||
10 ame,4) over (partition by d.deptno
11 order ame) ||', '||
12 ame,5) over (partition by d.deptno
13 order ame),', ') emps,
14 row_number () over (partition by d.deptno
15 order ame) x
16 from emp e, dept d
17 where d.deptno = e.deptno
18 )
19 where x = 1
20 /
DEPTNO DNAME EMPS
------- ----------- ------------------------------------------
10 ACCOUNTING CLARK, KING, MILLER
20 RESEARCH ADAMS, FORD, JONES, ROONEY, SCOTT, SMITH
30 SALES ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD
oracle FAQ
Q1.怎样创建表?
A. CREATE TABLE ROYAL_MTABLE
(
RM_INT_FIELD INTEGER,
RM_STR_FIELD VARCHAR2(64)
)
CREATE TABLE ROYAL_DTABLE
(
RD_INT_FIELD INTEGER,
RD_STR_FIELD VARCHAR2(32)
)
Q2.怎样删除表?
A. DROP TABLE ROYAL_DTABLE;
Q3.怎样创建视图?
A. CREATE OR REPLACE VIEW ROYAL_MDVIEW AS
SELECT T1.RM_STR_FIELD AS F1, T2.RD_STR_FIELD AS F2 FROM ROYAL_MTABLE T1, ROYAL_DTABLE T2
WHERE T1.RM_INT_FIELD = T2.RM_INT_FIELD
Q4.怎样删除视图?
A. DROP VIEW ROYAL_MDVIEW;
Q5.怎样给表添加字段?
A. ALTER TABLE ROYAL_DTABLE ADD RM_INT_FIELD INTEGER;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论