34,590
社区成员
发帖
与我相关
我的任务
分享
insert into tb
select 9999, 345, '01', 0, 1
select * from tb
/*
id p_id s_id amt flag
----------- ----------- ---------- --------------------------------------- ----
8423 1532 02 0.00 1
8438 1235 02 0.00 1
8448 104 02 0.00 1
8449 104 03 0.00 1
8450 104 19 20.00 1
8451 104 100 80.00 1
8458 1539 03 0.00 1
8795 421 02 0.00 1
8796 421 03 0.00 1
8797 421 01 0.00 1
9999 345 01 0.00 1
*/
select tb1.* from tb as tb1 where tb1.p_id in
(select tb2.p_id from tb as tb2 group by tb2.p_id having count(*)=1) and
tb1.s_id in ('02','03')
/*
结果:
id p_id s_id amt flag
----------- ----------- ---------- --------------------------------------- ----
8423 1532 02 0.00 1
8438 1235 02 0.00 1
8458 1539 03 0.00 1
(3 行受影响)
*/
if object_id('tb') is not null
drop table tb
go
create table tb(id int,p_id int,s_id varchar(10), amt numeric(8,2), flag tinyint)
insert into tb
select 8423,1532,'02', 0.00 ,1 union all
select 8438,1235,'02', 0.00 ,1 union all
select 8448 ,104 ,'02' ,0.00 ,1 union all
select 8449 ,104 ,'03' ,0.00 ,1 union all
select 8450 ,104 ,'19' ,20.00 ,1 union all
select 8451 ,104, '100' ,80.00 ,1 union all
select 8458 ,1539 ,'03', 0.00 ,1 union all
select 8795 ,421 ,'02' ,0.00, 1 union all
select 8796 ,421, '03', 0.00 ,1 union all
select 8797, 421 ,'01' ,0.00, 1
select tb1.* from tb as tb1 where tb1.p_id in
(select tb2.p_id from tb as tb2 group by tb2.p_id having count(*)=1) and
tb1.s_id in ('02','03')
/*
结果:
id p_id s_id amt flag
----------- ----------- ---------- --------------------------------------- ----
8423 1532 02 0.00 1
8438 1235 02 0.00 1
8458 1539 03 0.00 1
(3 行受影响)
*/
--理解错意思了
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-04-24 11:10:44
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[p_id] int,[s_id] varchar(3),[amt] numeric(4,2),[flag] int)
insert [tb]
select 8423,1532,'02',0.00,1 union all
select 8438,1235,'02',0.00,1 union all
select 8448,104,'02',0.00,1 union all
select 8449,104,'03',0.00,1 union all
select 8450,104,'19',20.00,1 union all
select 8451,104,'100',80.00,1 union all
select 8458,1539,'03',0.00,1 union all
select 8795,421,'02',0.00,1 union all
select 8796,421,'03',0.00,1 union all
select 8797,421,'01',0.00,1
--------------开始查询--------------------------
select
*
from
tb t
where
not exists (select 1 from tb where s_id <> '02' and s_id <> '03' and p_id=t.p_id )
-------------------------
/* id p_id s_id amt flag
----------- ----------- ---- --------------------------------------- -----------
8423 1532 02 0.00 1
8438 1235 02 0.00 1
8458 1539 03 0.00 1
(3 行受影响)
*/
select * from tab t where exists (select * from tab where p_id=t.p_id and t.s_id in ('03','02'))
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-04-24 11:10:44
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[p_id] int,[s_id] varchar(3),[amt] numeric(4,2),[flag] int)
insert [tb]
select 8423,1532,'02',0.00,1 union all
select 8438,1235,'02',0.00,1 union all
select 8448,104,'02',0.00,1 union all
select 8449,104,'03',0.00,1 union all
select 8450,104,'19',20.00,1 union all
select 8451,104,'100',80.00,1 union all
select 8458,1539,'03',0.00,1 union all
select 8795,421,'02',0.00,1 union all
select 8796,421,'03',0.00,1 union all
select 8797,421,'01',0.00,1
--------------开始查询--------------------------
select
*
from
tb t
where
exists(select 1 from tb where p_id=t.p_id and s_id ='02' or s_id ='03')
and
s_id in ('02','03')
----------------结果----------------------------
/* id p_id s_id amt flag
----------- ----------- ---- --------------------------------------- -----------
8423 1532 02 0.00 1
8438 1235 02 0.00 1
8448 104 02 0.00 1
8449 104 03 0.00 1
8458 1539 03 0.00 1
8795 421 02 0.00 1
8796 421 03 0.00 1
(7 行受影响)
*/
create table tb(id int,p_id int,s_id varchar(10),amt decimal(18,2),flag int)
insert into tb values(8423 ,1532 ,'02' , 0.00 ,1)
insert into tb values(8438 ,1235 ,'02' , 0.00 ,1)
insert into tb values(8448 ,104 ,'02' , 0.00 ,1)
insert into tb values(8449 ,104 ,'03' , 0.00 ,1)
insert into tb values(8450 ,104 ,'19' , 20.00 ,1)
insert into tb values(8451 ,104 ,'100', 80.00 ,1)
insert into tb values(8458 ,1539 ,'03' , 0.00 ,1)
insert into tb values(8795 ,421 ,'02' , 0.00 ,1)
insert into tb values(8796 ,421 ,'03' , 0.00 ,1)
insert into tb values(8797 ,421 ,'01' , 0.00 ,1)
go
select * from tb where p_id not in
(select distinct p_id from tb where s_id <> '02' and s_id <> '03' )
drop table tb
/*
id p_id s_id amt flag
----------- ----------- ---------- -------------------- -----------
8423 1532 02 .00 1
8438 1235 02 .00 1
8458 1539 03 .00 1
(所影响的行数为 3 行)
*/
select
*
from
tb t
where
exists(select 1 from tb where p_id=t.p_id and s_id ='02')
or
exists(select 1 from tb where p_id=t.p_id and s_id ='03')
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[p_id] int,[s_id] varchar(3),[amt] numeric(4,2),[flag] int)
insert [tb]
select 8799,105,'200',50.00,1 union all
select 8800,106,'02',30.00,1 union all
select 8801,106,'22',30.00,1 union all
select 8423,1532,'02',0.00,1 union all
select 8438,1235,'02',0.00,1 union all
select 8450,104,'19',20.00,1 union all
select 8451,104,'100',80.00,1 union all
select 8458,1539,'03',0.00,1 union all
select 8795,421,'02',0.00,1 union all
select 8796,421,'03',0.00,1 union all
select 8789,876,'02',0.00,1
--------------开始查询--------------------------
select * from tb where p_id not in(select p_id from tb where s_id not in('02','03'))
/*
8423 1532 02 .00 1
8438 1235 02 .00 1
8458 1539 03 .00 1
8795 421 02 .00 1
8796 421 03 .00 1
8789 876 02 .00 1
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[p_id] int,[s_id] varchar(3),[amt] numeric(4,2),[flag] int)
insert [tb]
select 8423,1532,'02',0.00,1 union all
select 8438,1235,'02',0.00,1 union all
select 8448,104,'02',0.00,1 union all
select 8449,104,'03',0.00,1 union all
select 8450,104,'19',20.00,1 union all
select 8451,104,'100',80.00,1 union all
select 8458,1539,'03',0.00,1 union all
select 8795,421,'02',0.00,1 union all
select 8796,421,'03',0.00,1 union all
select 8797,421,'01',0.00,1
--------------开始查询--------------------------
select * from tb where p_id not in(select p_id from tb where s_id not in('02','03'))
/*
结果:
id p_id s_id amt flag
----------- ----------- ---------- --------------------------------------- ----
8423 1532 02 0.00 1
8438 1235 02 0.00 1
8458 1539 03 0.00 1
(3 行受影响)
*/
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[p_id] int,[s_id] varchar(3),[amt] numeric(4,2),[flag] int)
insert [tb]
select 8799,105,'200',50.00,1 union all
select 8800,106,'02',30.00,1 union all
select 8801,106,'22',30.00,1 union all
select 8423,1532,'02',0.00,1 union all
select 8438,1235,'02',0.00,1 union all
select 8450,104,'19',20.00,1 union all
select 8451,104,'100',80.00,1 union all
select 8458,1539,'03',0.00,1 union all
select 8795,421,'02',0.00,1 union all
select 8796,421,'03',0.00,1 union all
select 8789,876,'02',0.00,1
--------------------------------查询开始------------------------------
select * from [tb] a where s_id in('02','03')
and not exists (select 1 from tb where s_id in('02','03') and p_id=a.p_id and s_id<>a.s_id)
/*
id p_id s_id amt flag
----------- ----------- ---- --------------------------------------- -----------
8800 106 02 30.00 1
8423 1532 02 0.00 1
8438 1235 02 0.00 1
8458 1539 03 0.00 1
8789 876 02 0.00 1
(5 行受影响)
*/