select * from tb t where exists(select 1 from tb where col2=t.col2 and col1<>t.col1)
解决方案 »
- 鹏哥,来给我搞个js,最后10分送给你
- 这个子查询怎样优化
- 统计:男女比例
- 如何用一个SQL语句取出记录号???
- 如何使用sql语句强制还原数据库?
- 展现progress数据库数据,是不是不支持isnull,Upper等函数,case when是不是也不能用
- 获取用户可访问的数据库
- 为什么连接SQL server的时候选择windows身份验证可以,用SQL Server身份验证就不行呢?
- what meanig of 'aa' and 'bb'in the SQL??
- microsoft Ado Data control 控件的问题
- 求教从几个表中选择需要的字段的存储过程怎么写啊
- sql server_查询结果合并列!请各位大大求救!
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-29 12:48:34
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([COL1] varchar(1),[COL2] int)
insert [tb]
select 'A',11 union all
select 'B',11 union all
select 'C',12 union all
select 'D',13
--------------开始查询--------------------------
select * from tb t where exists(select 1 from tb where col2=t.col2 and col1<>t.col1)
----------------结果----------------------------
/* COL1 COL2
---- -----------
A 11
B 11(2 行受影响)*/
SELECT * FROM tb abc WHERE (SELECT COUNT(*)FROM tb WHERE tb.COL2=abc.col2)>1
SELECT a.*
FROM 表 a
JOIN
(
SELECT COL2
FROM 表
GROUP BY COL2
HAVING COUNT(*)>1
) b ON a.COL2=b.COL2