sql >> データベース >  >> RDS >> Sqlserver

2つの列で同じテーブルにフィールドが相互に存在するかどうかを確認する方法

    このようなものはあなたのために働きますか:

    With ManagerWorkers As
        (
        -- get managers with workers
        Select Managers.WorkerUsername As ManagerUsername, Workers.WorkerUsername
        From tblMyTable As Managers
            Join tblMyTable As Workers
                On Workers.ManagerUsername = Managers.WorkerUsername
        ) 
    Select *
    From ManagerWorkers
    Union All
    -- get workers that have a manager in the above list
    Select WorkerUsername, ManagerUsername
    From tblMyTable
    Where Exists(   Select 1
                    From ManagerWorkers
                    Where ManagerWorkers.ManagerUsername = tblMyTable.ManagerUsername
                    )   
    

    編集 :質問の更新を考えると、次のクエリはどうですか:

    Select D1.u_username, U1.Permission, U1.Grade, D1.f_username, U2.Permission, U2.Grade
    from tblDynamicUserList As D1
        Join tblDynamicUserList As D2
            On D2.u_username = D1.f_username
                And D2.f_username = D1.u_username
        Join tblUsers As U1
            On U1.u_username = D1.u_username
        Join tblUsers As U2
            On U2.u_username = D2.u_username
    


    1. 複数の名前付きパラメーターを持つPDObindParam()

    2. Mysqlユーザー作成スクリプト

    3. 毎分cronjob

    4. PostgreSQLの複数の列からの月間合計の表示