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
from a where 1<>1 3
.between
的用法,between
限制查詢資料範圍時包括了邊界值
,not between
不包括
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between
數值1 and 數值2
4. 說明:包括所有在
tablea
中但不在
tableb
和tablec
中的行並消除所有重複行而派生出乙個結果表
(select a from
tablea
) except (select a from
tableb
) except (select a from
tablec)
5. 初始化表,可以將自增長表的字增長欄位置為1
truncate table table1 6
.多語言設定資料庫或者表或者
order by
的排序規則 --
修改使用者資料庫的排序規則
ater database dbname collate sql_latin1_general_cp1_ci_as --
修改欄位的排序規則
alter table a alter column c2 varchar(50) collate sql_latin1_general_cp1_ci_as --
按姓氏筆畫排序
select * from
表名order by
列名collate chinese_prc_stroke_ci_as --
按拼音首字母排序
select * from
表名order by
列名collate chinese_prc_cs_as_ks_ws 7
.列出所有的使用者資料表:
select top 100 percent o.name as 表名
from dbo.syscolumns c inner join
dbo.sysobjects o on o.id = c.id and objectproperty(o.id, n'isusertable') = 1 and
o.name <> 'dtproperties' left outer join
dbo.sysproperties m on m.id = o.id and m.smallid = c.colorder
where (c.colid = 1)
order by o.name, c.colid
8.列出所有的使用者資料表及其字段資訊:
select top 100 percent c.colid as
序號, o.name as
表名, c.name as
列名,
t.name as
型別, c.length as
長度, c.isnullable as
允許空,
cast(m.[value] as varchar(100)) as 說明
from dbo.syscolumns c inner join
dbo.sysobjects o on o.id = c.id and objectproperty(o.id, n'isusertable') = 1 and
o.name <> 'dtproperties' inner join
dbo.systypes t on t.xusertype = c.xusertype left outer join
dbo.sysproperties m on m.id = o.id and m.smallid = c.colorder
order by o.name, c.colid 9
.left
,right join
的另外一種簡潔的寫法
select * from a,b where a.id *= b.id--
(*=
相當於left join
)select * from a,b where a.id =* b.id--
(=*
相當於right join)10
.update from
和delete from 11
.得到表中最小的未使用的id號
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) 12
.隨機取得記錄
select top 10 * from t1 order by newid()
精妙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語句
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,...