精妙SQL語句

2021-05-08 04:19:37 字數 2260 閱讀 4332

1

. 說明:複製表(只複製結構,源表名:a,新錶名:b)

sql:

select

*into

b from

a where

1<>1;

2. 說明:拷貝表(拷貝資料,源表名:a,目標表名:b)

sql:

insert

into

b(a, b, c)

select

d, e, f

from

b; 3

select

a.title, a.username, b.adddate

from

table

a,(select

max(adddate) adddate

from

table

where

table

.title

=a.title) b  

4. 說明:外連線查詢(表名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;  

5select

*from

日程安排

where

datediff

(』』minute』』, f開始時間,

getdate

())>56

. 說明:兩張關聯表,刪除主表中已經在副表中沒有的資訊

sql:

delete

from

info

where

notexists

(select

*from

infobz

where

info.infid

=infobz.infid );

7. 說明:——

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 (+)

andx.inbound_qty

+nvl(y.stock_onhand,0)

<>

x.stock_onhand ) b

where

a.num

=b.num; 

8. 說明:——

sql:

select

*from

studentinfo

where

notexists

(select

*from

student

where

studentinfo.id

=student.id)

and系名稱

=』』"

&strdepartmentname

&"』』

and專業名稱

=』』"

&strprofessionname

&"』』

order

by性別, 生源地, 高考總成績;

精妙SQL語句

精妙sql語句 說明 複製表 只複製結構 源表名 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....

精妙Sql語句

1 判斷a 表中有而 b表中沒有的記錄 select a.from tbl1 a left join tbl2 b on a.key b.key where b.key is null 雖然使用 in也可以實現,但是這種方法的效率更高一些 2 新建乙個與某個表相同結構的表 select into b...

精妙SQL語句

10 sql server中直接迴圈寫入資料 declare i int set i 1 while i 30 begin insert into test userid values i set i i 1 end小記儲存過程中經常用到的本週,本月,本年函式 dateadd wk,datediff...