选择所有具有相同薪水的人员

时间:2014-05-30 11:06:39

标签: sql select equals

我在查找SQL查询的解决方案时遇到问题。这可能是一个非常明显的初学者问题,但我似乎无法得到我之后的结果。我有一个类似于以下内容的表:

|Name |Station|Salary|
|Bob  |1      |2000  |
|Steve|2      |1750  |
|Mark |3      |2050  |
|Lisa |4      |2200  |
|Hans |5      |2000  |

我想选择此表中具有相同薪水的人员姓名。结果当然应该是 Bob Hans

12 个答案:

答案 0 :(得分:5)

如果你在薪水上加入自己的表格,但名字是分开的,那么这应该给你任何匹配的受薪人员:

SELECT s1.Name, s1.Station, s1.Salary
FROM Staff s1
INNER JOIN Staff s2 ON s1.Salary = s2.Salary AND s1.Name <> s2.Name

Here's a SQLFiddle以显示其实际效果

答案 1 :(得分:3)

SELECT Name
FROM table1 
WHERE Salary IN (
    SELECT Salary
    FROM table1
    GROUP BY Salary
    HAVING COUNT(*) > 1
)

答案 2 :(得分:3)

如果确定拥有多名员工的工资范围,则可以将其作为派生表重新加入:

SELECT e.Name, e.Station 
FROM Employee e
INNER JOIN
(
   SELECT Salary
   FROM Employee
   GROUP BY Salary
   HAVING COUNT(*) > 1
) grp ON e.Salary = grp.Salary;

SqlFiddle here

答案 3 :(得分:2)

大多数数据库都支持窗口功能。这样做的简单方法是计算具有给定薪水的人数。然后选择那些人。这是一个简单的方法:

select t.*
from (select t.*, count(*) over (partition by salary) as salarycnt
      from table t
     ) t
where salarycnt > 1
order by salary;

答案 4 :(得分:1)

使用以下查询:

with datatab as
(
 select 'Bob' Name,   1 Station, 2000  Salary from dual union
 select 'Steve' Name,   2 Station, 1750  Salary from dual union
 select 'Mark' Name,   3 Station, 2050  Salary from dual union
 select 'Lisa' Name,   4 Station, 2200  Salary from dual union
 select 'Hans' Name,   5 Station, 2000  Salary from dual union
 select 'Test' Name,   6 Station, 1750  Salary from dual
)

SELECT NAME, sTATION, SALARY  FROM DATATAB
WHERE SALARY IN 
(
   SELECT Salary
   FROM datatab
   GROUP BY Salary
   HAVING COUNT(1) > 1
);

正如有人在Edits中建议的那样,The Query将是:

SELECT NAME, sTATION, SALARY  FROM TABLE_NAME
WHERE SALARY IN 
(
   SELECT Salary
   FROM TABLE_NAME
   GROUP BY Salary
   HAVING COUNT(1) > 1
)

答案 5 :(得分:0)

尝试这个:

SELECT E1.Name, E1.Salary
FROM Employee E1, Employee E2
WHERE E1.Salary = E2.Salary
AND E1.Name <> E2.Name

http://sqlfiddle.com/#!2/1e34b

答案 6 :(得分:0)

从中选择a.name,a.salary ( 选择计数(工资)作为cnt,工资组的工资由有工资的工资(工资)&gt; 1

)作为X. 内部联接人员a a.Salary = x.salary

按顺序排序:

答案 7 :(得分:0)

当您必须将同一个表放两次或更多次时,请使用别名

select t1.Name,
       t2.Name,
       t1.Salary 
  from MyTable t1,
       MyTable t2
 where t1.Station > t2.Station and -- if Station is id 
       -- t1.Name > t2.Name -- if Name is in fact an id
       t1.Salary = t2.Salary 

答案 8 :(得分:0)

试试这个:

double start = omp_get_wtime();

            #pragma omp parallel for collapse(2)
            for(int y=0;y<hl;y++) 
                for(int x=0;x<wl;x++) 
                {
                    dI_l[x + y*wl][0] = 0.25f * (dI_lm[2*x   + 2*y*wlm1][0] +
                                                dI_lm[2*x+1 + 2*y*wlm1][0] +
                                                dI_lm[2*x   + 2*y*wlm1+wlm1][0] +
                                                dI_lm[2*x+1 + 2*y*wlm1+wlm1][0]);
                }

            double end = omp_get_wtime();
            std::cout << "   wall time : " << end - start << std::endl;

答案 9 :(得分:0)

对于MySQL:

从工作人员中选择* 工资在哪里(从工人那里选择工资 按薪水分组 具有count(1)> 1);

答案 10 :(得分:0)

让表名称为:员工

选择唯一的e.name,例如雇员e,雇员e1的薪水,其中 e.salary = e1.salary和e.name!= e1.name;

答案 11 :(得分:-1)

您是否想要拥有相同工资的人数。

SELECT count(*) FROM table GROUP BY salary