Answer the question
In order to leave comments, you need to log in
SQL two join in the same table?
Hello, there is an items table:
item_id uniqueidentifier not null constraint pk_items primary key,
firstDocument uniqueidentifier not null,
lastDocument uniqueidentifier not null
docid uniqueidentifier not null constraint pk_docs primary key,
op_type int not null
select
i.item_id,
i.firstDocument,
fd.op_type,
i.lastDocument,
ld.op_type
from
items i
left join documents fd on fd.docid = i.firstDocument
left join documents ld on ld.docid = i.lastDocument
Answer the question
In order to leave comments, you need to log in
SELECT items.item_id AS Item, FirstDoc.op_type AS FirstDocOper, LastDoc.op_type AS LastDocOper
FROM items
INNER JOIN documents AS FirstDoc ON items.firstDocument = FirstDoc.docid
INNER JOIN documents AS LastDoc ON items.lastDocument = LastDoc.docid
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question