Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Cara memeriksa keberadaan bersama Bidang dalam tabel yang sama di Dua kolom

Apakah sesuatu seperti ini cocok untuk Anda:

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
                )   

EDIT :Mengingat pembaruan untuk pertanyaan bagaimana dengan kueri berikut:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Pesan Kesalahan Parser:Nama koneksi 'LocalSqlServer' tidak ditemukan dalam konfigurasi aplikasi atau string koneksi kosong

  2. Apa ruang lingkup CONTEXT_INFO di SQL Server?

  3. Mengapa saya tidak bisa menggunakan alias untuk agregat dalam klausa yang memiliki?

  4. SQL Server 2008 dan HashBytes

  5. Bagaimana mendeteksi jika prosedur tersimpan sudah ada