I have a join probelm with one of my statements.
The scenario is that each document has stock assigned to it and that stock may or may not have serial no.s assigned to it. I need to select the every model id which is on a document, along with the serial no.s assigned to the model id if there are any assigned.
select count (docstk.id)
from doc, docstk, stock,
where doc.id = 442 and
docstk.docid = doc.id and
docstk.stkid = stock.id
gives me the 26 stock ids I need from the tables. However I also need to select the serial no's which have been assigned to the stock id's within the document.
Using this query I get a cartesian product
select docstk.id, serial.serno
from doc, docstk, stock, serial, docser
where doc.id = 442 and
docstk.docid = doc.id and
docstk.stkid = stock.id and
serial.id = docser.serid
and using this query I only get the three models which have seril no's assigned to them (I also need to see the models which have no serial no.s assigned to them).
SELECT Serial.serno, Stock.code, Doc.id
FROM DocSer ,DocStk,Serial,Stock,Doc
WHERE Doc.id = 442
AND Doc.id = DocStk.DocID
AND DocStk.id = DocSer.DocStkID
AND DocSer.SerID = Serial.id
AND DocStk.StkID = Stock.id
I know I need to use some sort of inner/left(??) join on the tables but I am unsure of the syntax and getting nowhere fast.
I hope somebody can help me as I am really stuck and have an iminent deadline.
Thanks in advance
marabou
The scenario is that each document has stock assigned to it and that stock may or may not have serial no.s assigned to it. I need to select the every model id which is on a document, along with the serial no.s assigned to the model id if there are any assigned.
select count (docstk.id)
from doc, docstk, stock,
where doc.id = 442 and
docstk.docid = doc.id and
docstk.stkid = stock.id
gives me the 26 stock ids I need from the tables. However I also need to select the serial no's which have been assigned to the stock id's within the document.
Using this query I get a cartesian product
select docstk.id, serial.serno
from doc, docstk, stock, serial, docser
where doc.id = 442 and
docstk.docid = doc.id and
docstk.stkid = stock.id and
serial.id = docser.serid
and using this query I only get the three models which have seril no's assigned to them (I also need to see the models which have no serial no.s assigned to them).
SELECT Serial.serno, Stock.code, Doc.id
FROM DocSer ,DocStk,Serial,Stock,Doc
WHERE Doc.id = 442
AND Doc.id = DocStk.DocID
AND DocStk.id = DocSer.DocStkID
AND DocSer.SerID = Serial.id
AND DocStk.StkID = Stock.id
I know I need to use some sort of inner/left(??) join on the tables but I am unsure of the syntax and getting nowhere fast.
I hope somebody can help me as I am really stuck and have an iminent deadline.
Thanks in advance
marabou