SQLは勉強します重複データの照会と複数のテーブルデータの接続方法

2559 ワード

データベーステストを行うには、発見された問題が汚いデータであるかどうかを確認するために、シーンごとにデータを検索する必要があります。最近よく使われている照会方法を記録してください。
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