Search code examples
sqlsql-serversql-server-2019

Obtain names from foreign key relationship


I would like to alter my query result to give me a different result for example

when I run this query

select *
from receipt
where receiptId = 358914592`

It gives me a table like this

receiptId date price salesRep PatientId stat reqDoc reviewwee patho medtech
358914592 2023-06-21 1200 NULL 1023957457 done dr. Dave 10111012317 64242 32347

I want the query to show the names of the pathologist, reviewee and medtech rather than their primary keys

like this

receiptId date price salesRep PatientId stat reqDoc reviewwee patho medtech
358914592 2023-06-21 1200 NULL 1023957457 done dr. Dave nurse Joy Dr. Hawk browns michaela jose

Solution

  • You need to use Joins in order to display that information.

    select receiptId, date, price, patient,..., path.name...
    from receipt as rec
    join pathologist as path on rec.pathId = path.Ud
    where receiptId = 358914592`