Deniss Muntjans
Deniss Muntjans

Reputation: 379

Select data depending on data from the same table

I can not figure how to select data depending on data from the same table.

I've got this table:

id  claimant  approver_id
1       A         4
2       B         4
3       C         1
4       D         1

I would like to get approvers but with names rather than id's. How can I achieve this?

It should be something like this:

id  claimant  approver_id  approver
1       A         4           D
2       B         4           D
3       C         1           A
4       D         1           A

Upvotes: 0

Views: 38

Answers (2)

SIDU
SIDU

Reputation: 2278

SELECT a.id, a.claimant, a.approver_id, b.claimant AS approver
FROM tab AS a
JOIN tab AS b ON a.approver_id = b.id

Upvotes: 1

Kamil Gosciminski
Kamil Gosciminski

Reputation: 17137

Use JOIN to the same table with conditions and alias your column to name it differently for the approver. This is called self join.

select t.claimant, t.approver_id, k.claimant AS approver
from yourtable t
join yourtable k on t.approver_id = k.id

Upvotes: 1

Related Questions