我有一个这样的查询:
select a.id, a.color, a.shade from colors a where a.color = 'red'哪个取
ID | Color | shade
-------|------------|---------
23 |red | dark10
525 |red | light-10问题:
如何在表colors中找到所有记录,其中有多个(两个)相同的color出现,而它们对应的阴影并不相同
像下面这样的记录将不予考虑
ID | Color | shade
-------|------------|---------
23 |green | light-10
324 |green | light-10发布于 2011-12-27 07:09:03
您可以使用一个GROUP子句来完成这一任务,该子句的条件是有COUNT(*) =1:
declare @colors table (
id int,
color nvarchar(100),
shade nvarchar(100)
)
insert into @colors
select 23, 'red', 'dark10'
union all select 525, 'red', 'light-10'
union all select 23, 'green', 'light-10'
union all select 324, 'green', 'light-10'
select c.*
from @colors c
inner join (
select color, shade
from @colors
group by color, shade
having COUNT(*) > 1
) x
on x.color = c.color
and x.shade <> c.shadehttps://stackoverflow.com/questions/8641940
复制相似问题