本博客總結自各個博客,聚集於此。不按期更新。。。java
name kecheng fenshu面試
張三 語文 81sql
張三 數學 75數據庫
李四 語文 76oracle
李四 數學 90jsp
王五 語文 81設計
王五 數學 100code
王五 英語 90xml
解法1: select distinct name from table where name not in (select distinct name from table where fenshu<=80)
解法2:select name from table group by name having min(fenshu)>80get
學生表 以下:
自動編號 學號 姓名 課程編號 課程名稱 分數
1 2005001 張三 0001 數學 69
2 2005002 李四 0001 數學 89
3 2005001 張三 0001 數學 69
Answer: delete tablename where 自動編號 not in(select min( 自動編號) from tablename group by 學號, 姓名, 課程編號, 課程名稱, 分數)
答:select a.name, b.name
from team a, team b
where a.name < b.name
AccID :科目代碼,Occmonth :發生額月份,DebitOccur :發生額。
數據庫名:JcyAudit ,數據集:Select * from TestDB
答:select a.*
from TestDB a
,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur
year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成這樣一個結果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
select year,
(select amount from aaa m where month=1 and m.year=aaa.year) as m1,
(select amount from aaa m where month=2 and m.year=aaa.year) as m2,
(select amount from aaa m where month=3 and m.year=aaa.year) as m3,
(select amount from aaa m where month=4 and m.year=aaa.year) as m4
from aaa group by year
SQL: select * into b from a where 1<>1 (where1=1,拷貝表結構和數據內容)
ORACLE:create table b
As
Select * from a where 1=2
補充
一、既複製表結構也複製表內容的SQL語句:CREATE TABLE tab_new AS SELECT * FROM tab_old;
二、只複製表結構不復製表內容的SQL語句:CREATE TABLE tab_new AS SELECT * FROM tab_old WHERE 1=2;
三、不復製表結構,只複製內容的sql語句:insert into tab_new select * from tab_old;或者SELECT vale1, value2 into Table2 from Table1
SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
SQL: select * from 日程安排 where datediff('minute',f 開始時間,getdate())>5
這道題的SQL 語句怎麼寫?
update b set b.value=(select a.value from a where a.key=b.key) where b.id in(select b.id from b,a where b.key=a.key);
原表:
courseid coursename score
-------------------------------------
1 java 70
2 oracle 90
3 xml 40
4 jsp 30
5 servlet 80
-------------------------------------
爲了便於閱讀, 查詢此表後的結果顯式以下( 及格分數爲60):
courseid coursename score mark
---------------------------------------------------
1 java 70 pass
2 oracle 90 pass
3 xml 40 fail
4 jsp 30 fail
5 servlet 80 pass
---------------------------------------------------
寫出此查詢語句
select courseid, coursename ,score ,decode (sign(score-60),-1,'fail','pass') as mark from course
create table testtable1
(
id int IDENTITY,
department varchar(12)
)
select * from testtable1
insert into testtable1 values('設計')
insert into testtable1 values('市場')
insert into testtable1 values('售後')
結果
id department
1 設計
2 市場
3 售後
create table testtable2
(
id int IDENTITY,
dptID int,
name varchar(12)
)
insert into testtable2 values(1,'張三')
insert into testtable2 values(1,'李四')
insert into testtable2 values(2,'王五')
insert into testtable2 values(3,'彭六')
insert into testtable2 values(4,'陳七')
用一條SQL語句,怎麼顯示以下結果
id dptID department name
1 1 設計 張三
2 1 設計 李四
3 2 市場 王五
4 3 售後 彭六
5 4 黑人 陳七
答案:
SELECT testtable2.* , ISNULL(department,'黑人')
FROM testtable1 right join testtable2 on testtable2.dptID = testtable1.ID
也作出來了可比這方法稍複雜。
select top 10 * from A where ID >(select max(ID) from (select top 30 ID from A order by A ) T) order by A
select * from(select count(ID) as count from table group by ID)T where T.count>3