34,590
社区成员
发帖
与我相关
我的任务
分享
use tempdb
if object_id('tb') is not null drop table tb
go
create table tb([id] INT,[name] varchar(50),[sign] INT)
insert into tb
select 1,'a',1 union all
select 2,'b',1 union all
select 3,'c',1 union all
select 4,'c',8 union all
select 5,'a',7 union all
select 6,'a',6
go
select * from tb t
where not exists(select * from tb where t.name = name and sign <> 1)
select * from tb t where sign=1 and
(select count(1) from tb where name=t.name)=1
---查询---
select *
from tb t
where (select count(1) from tb where name=t.name)=1
and sign=1
---结果---
id name sign
----------- ---- -----------
2 b 1
(所影响的行数为 1 行)
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[name] varchar(1),[sign] int)
insert [tb]
select 1,'a',1 union all
select 2,'b',1 union all
select 3,'c',1 union all
select 4,'c',8 union all
select 5,'a',7 union all
select 6,'a',6
go
select *
from tb k
where
not exists(select * from tb where k.name=name and k.id<>id or k.name=name and sign<>1)
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-17 17:00:43
-- 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.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[name] varchar(1),[sign] int)
insert [tb]
select 1,'a',1 union all
select 2,'b',1 union all
select 3,'c',1 union all
select 4,'c',8 union all
select 5,'a',7 union all
select 6,'a',6
--------------开始查询--------------------------
select
*
from
tb t
where
not exists(select * from tb where name=t.name and [sign] <> 1)
----------------结果----------------------------
/*id name sign
----------- ---- -----------
2 b 1
(1 行受影响)
*/
create table tb(id int, name varchar(10), sign int)
insert into tb values(1 , 'a' , 1 )
insert into tb values(2 , 'b' , 1 )
insert into tb values(3 , 'c' , 1 )
insert into tb values(4 , 'c' , 8 )
insert into tb values(5 , 'a' , 7 )
insert into tb values(6 , 'a' , 6 )
GO
SELECT * FROM TB where sign = 1 and name in (select name from tb group by name having count(1) = 1)
DROP TABLE TB
/*
id name sign
----------- ---------- -----------
2 b 1
(所影响的行数为 1 行)
*/
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-17 17:00:43
-- 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.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[name] varchar(1),[sign] int)
insert [tb]
select 1,'a',1 union all
select 2,'b',1 union all
select 3,'c',1 union all
select 4,'c',8 union all
select 5,'a',7 union all
select 6,'a',6
--------------开始查询--------------------------
select * from tb where name in (select name from tb group by name having count(1)=1)
----------------结果----------------------------
/*id name sign
----------- ---- -----------
2 b 1
(1 行受影响)
*/
select * from tb as a
where not exists(select * from tb
where a.name=b.name and sign <> 1)
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[name] varchar(1),[sign] int)
insert [tb]
select 1,'a',1 union all
select 2,'b',1 union all
select 3,'c',1 union all
select 4,'c',8 union all
select 5,'a',7 union all
select 6,'a',6
---查询---
select *
from tb t
where (select count(1) from tb where name=t.name)=1
---结果---
id name sign
----------- ---- -----------
2 b 1
(所影响的行数为 1 行)
SELECT * FROM TB T WHERE ID=(SELECT MAX(ID) FROM TB WHERE SIGN=T.SIGN) AND SING=1