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

 

 

SQL学习_查询重复数据和连接多个表数据的方法,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。