显示验证消息,其中包含重复的StudentId一对一

时间:2019-05-29 11:10:19

标签: mysql sql

我想在SQL Server中检查重复的ID验证,该验证网格显示 行号,验证消息和来自SQL Server的重复ID

我不明白该怎么办?

非常感谢帮助

创建表格

    CREATE TEMPORARY TABLE IF NOT EXISTS Tbl_Student
    (RowID INT PRIMARY KEY auto_increment, StudentID BIGINT);

插入记录

Insert into Tbl_Student(RowID,StudentID) values (1,101)
Insert into Tbl_Student(RowID,StudentID) values (2,102)
Insert into Tbl_Student(RowID,StudentID) values (3,101)
Insert into Tbl_Student(RowID,StudentID) values (4,102)
Insert into Tbl_Student(RowID,StudentID) values (5,103)


enter image description here

请在那里分享任何解决方案

谢谢

5 个答案:

答案 0 :(得分:1)

尝试以下查询,

SELECT RowID, CONCAT('StudentID ',StudentID, ' is Duplicate) AS Error FROM Tbl_Student WHERE StudentID IN (SELECT StudentID FROM Tbl_Student GROUP BY StudentID  HAVING COUNT(*) > 1)

答案 1 :(得分:0)

这是T-SQL代码,您可能需要将其转换为MySql格式:

Select * from(
      Select RowId, Count(*) Over(Partition By StudentId Order By RowId) as Cnt From [YourTable]
) as K
Where Cnt>1

您也可以使用Row_NumberCount聚合函数来达到相同的结果。

这里有很多MySql解决方案:

Find duplicate records in MySQL

答案 2 :(得分:0)

要显示重复的内容,

select
  RowID, 
  concat('Student ', StudentID, ' is duplicate') as StudentID
from Tbl_Student where StudentID in (
  select StudentID from Tbl_Student group by StudentID having count(*) > 1
)

答案 3 :(得分:0)

您可以在suquery上使用联接编号为1的链结

  select id,  student_id, concat('Stundent id' , student_id, ' is duplicated')
  from  my_table m
  inner join  (
    select student_id 
    from my_table  
    group by student_id 
    having count(*) > 1  
  ) t on t.student_id  = m.student_id

答案 4 :(得分:0)

我认为这对您有帮助

select 
      RowID
      concat('Student ', StudentID, ' is duplicate') as Error
from 
      Tbl_Student 
where 
      StudentID in (
select 
      StudentID 
from 
      Tbl_Student 
group by 
      StudentID 
having 
      count(*) > 1);