首页 > 代码库 > SQL学习_查询重复数据和连接多个表数据的方法

SQL学习_查询重复数据和连接多个表数据的方法

进行数据库测试时需要根据不同场景查询数据,以便验证发现的问题是否为脏数据引起的。记录一下最近常用的查询方法:

1. 查询表中重复数据(id不同,多个字段值相同)

select P1.* 
from project as P1, project as P2
where P1.id<>P2.id and P1.ProjectId=P2.ProjectId and P1.ServiceTypeId=P2.ServiceTypeId and P1.Rank=P2.Rank

 

2.连接多个表数据

select Name, FirstName, LastName, HoursWorked
from employee as E, project as P, assignment as A
where E.EmployeeNumber=A.EmployeeNumber and P.projectID=A.ProjectID
order by P.ProjectID, A.EmployeeNumber

或:

select Name, FirstName, LastName, HoursWorked
from employee as E join assignment as A 
        on  E.EmployeeNumber=A.EmployeeNumber
            join project as P  
                on P.projectID=A.ProjectID
order by P.ProjectID, A.EmployeeNumber