简体   繁体   English

MySQL选择具有相同列值的行

[英]MySQL Select the rows having same column value

I need to obtain the rows which having same column value from the following table, I tried it the following way, but it gives me a single row only. 我需要从下表中获取具有相同列值的行,我按照以下方式尝试,但它只给我一行。

select *
from employee e
group by e.empsalary
having count(e.empsalary) > 1

Table employee 表员工

在此输入图像描述

Please suggest me a way. 请建议我一个方法。

You should be able to accomplish this by joining the table to itself: 您应该能够通过将表连接到自身来实现此目的:

SELECT
    a.*
FROM
    employee a
    JOIN employee b
        ON a.empsalary = b.empsalary
        AND a.empid != b.empid

Use an inner join to join with your query posted. 使用内部联接加入您发布的查询。

select A.* from employee A
inner join (
  select empsalary
  from employee 
  group by empsalary
  having count(*) > 1
) B ON A.empsalary = B.empsalary

Something like this perhaps 或许这样的事情

SELECT * FROM employee a
WHERE EXISTS (
    SELECT 1 FROM employee b
    WHERE a.empsalary = b.empsalary
    AND a.empid <> b.empid
);

Demo - http://sqlfiddle.com/#!2/d75d9/4 演示 - http://sqlfiddle.com/#!2/d75d9/4

select * from employee where empsalary IN(select empsalary from employee  group by empsalary having count(empsalary ) > 1)

Try This.It gives 2 rows as you want. 试试This.It你想要的两行。

Demo: http://sqlfiddle.com/#!2/d75d9/6 演示: http//sqlfiddle.com/#!2/d75d9/6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM