如何从单个表中选择多列值相同的行 [英] How to select multiple columns values same rows from single table
问题描述
我有一个SQL Server表.现在该表具有像主键Id
,A, B, C, D, E, F, G
I have a SQL Server table. Now this table has columns like primary key Id
, A, B, C, D, E, F, G
现在我要像这样从表中选择行
Now I want to select rows from this table like this
A=A, B=B, C=C, D=D and G > 132
因此,我尝试从此表中选择行,其中行A,B,C,D
列具有相同的数据,而G列数据> 132.
So I am trying to select rows from this table which rows A,B,C,D
columns has same data and G column data > 132.
那我该怎么做呢?谢谢.
So how can I do that ? Thank you.
我尝试了此查询,但返回了相同的ID行
I tried this query but returning same Id rows
SELECT TableA.Id,TableA.UserId,TableA.MaximumHp,TableA.Attack,TableA.Defense,TableA.SpAttack,TableA.SpDefense,TableA.Speed
FROM myTable as TableA
Inner Join myTable as TableB on
TableA.MaximumHp = TableB.MaximumHp
AND TableA.Attack = TableB.Attack
AND TableA.Defense = TableB.Defense
AND TableA.SpAttack = TableB.SpAttack
AND TableA.SpDefense = TableB.SpDefense
AND TableA.Speed = TableB.Speed
AND TableA.Id != TableB.Id
SQL Server 2008 R2
SQL Server 2008 R2
推荐答案
我认为您的意思是重复项.告诉我这是否是您要寻找的东西.
I THINK what you mean is duplicates. Tell me if this is what you are looking for.
SELECT [Table].A, [Table].B, [Table].C, [Table].D, [Table].E, [Table].F, [Table].G
FROM [Table] LEFT JOIN (SELECT A, B, C, D FROM [Table]
GROUP BY A, B, C, D
HAVING count(*) > 1)
AS sub ON ([Table].A=sub.A) AND ([Table].B=sub.B) AND ([Table].C=sub.C) AND ([Table].D=sub.D)
WHERE G>132 and sub.A is not null;
这将为您提供a,b,c和D等于表中另一行的所有行...并且G> 132
This will give you all the rows where a,b,c, and D are equal to another row in the table...and G > 132
这篇关于如何从单个表中选择多列值相同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!