SQL研究 MS SQL 之一 篩選資料

2021-04-21 02:43:25 字數 1628 閱讀 8288

摘抄自論壇資源:http://topic.csdn.net/u/20081023/13/6d3b2fd6-39fa-4efe-9fd2-d1ea11b08091.html

要求如下:

table1

欄位1  欄位2

1001  true

1003  false

1006  false

1002  true

1002  false

1004  false

table2

欄位1  欄位2

1003  false

1006  false

1004  false

也就是從表1中篩選出欄位2從來沒有為true的記錄

實現:方法1:(臨時表,生成於tempdb下,查詢分析器下可以檢視到)

--> 生成測試資料: #t

if object_id('tempdb.dbo.#t') is not null drop table #t

create table #t (欄位1 int,欄位2 varchar(5))

insert into #t

select 1001,'true' union all

select 1003,'false' union all

select 1006,'false' union all

select 1002,'true' union all

select 1002,'false' union all

select 1004,'false'

--sql查詢如下:

select *

from #t as t

where not exists

(select * 

from #t

where 欄位1=t.欄位1

and 欄位2='true')/*

欄位1         欄位2

----------- -----

1003        false

1006        false

1004        false

(3 行受影響)

*/方法2:

declare @tb table(f1 int, f2 varchar(5))

insert @tb 

select 1001,  'true' union all 

select 1003,  'false' union all 

select 1006,  'false' union all 

select 1002,  'true' union all 

select 1002,  'false' union all 

select 1004,  'false'

select * from @tb  as a where not exists(select 1 from @tb where f1=a.f1 and f2='true')

/*f1          f2    

----------- ----- 

1003        false

1006        false

1004        false

*/

function object研究之一

function object首先是乙個類,它實現了函式呼叫操作符t operator t t可以是void型別。class a void operator int x cout x 0通過檢視for each的源 可以觀察到stl中使用function object的基本特徵。下面是gcc4.6....

function object研究之一

function object首先是乙個類,它實現了函式呼叫操作符t operator t t可以是void型別。class a void operator int x cout x 0通過檢視for each的源 可以觀察到stl中使用function object的基本特徵。下面是gcc4.6....

SQL優化之一

在sqlserver2008 management studio中執行下列 set statistics time on goselect from information.lhbinfo where tradingdate between 2010 01 01 and 2011 06 10 god...