我将如何加入这些表格?

时间:2016-05-25 03:45:07

标签: mysql sql database join

我在加入这些表格时遇到了问题;我有这个代码。

我的疑问:

SELECT partial a.{ediTransactionDetailId, poNumber},
       partial b.{edi997DetailId, noOfTrans},
       partial c.{ediTransactionId, senderId, receiverId, gsNumber, isaNumber, fileName},
       partial d.{ediDocTypeId, docType}
FROM
    MATRIXEDIBUNDLE:editransactiondetail a
    JOIN   a.edi997details b
    JOIN   b.editransaction c
    JOIN   c.edidoctype d
WHERE  c.filename LIKE :fileName
    AND    a.ponumber LIKE :poNumber
    AND    d.doctype = :docType
    AND    a.flag = 1
    AND    c.flag = 1 

我收到了这个错误:

  

加入b.ediTransaction':错误:类   Matrix \ MatrixEdiBundle \ Entity \ EdiTransactionDetail没有关联   名为edi997Details

我该如何加入?

tables as follows :

2 个答案:

答案 0 :(得分:1)

您需要学习 JOIN sintaxis

您需要ON clausule

FROM MatrixEdiBundle:EdiTransactionDetail a 
JOIN Details b
  ON a.SomeID  = B.SomeID

或者您需要CROSS JOIN

FROM MatrixEdiBundle:EdiTransactionDetail a 
CROSS JOIN Details b

答案 1 :(得分:1)

你没有在表之间建立关系,这就是你在这里得到错误的原因,

    SELECT table1.column1, table2.column2...
    FROM table1 JOIN table2
    ON table1.common_field = table2.common_field; // (This part is missing in your code)

进一步研究http://www.tutorialspoint.com/sql/sql-using-joins.htm

您的代码看起来应该是这样的,

SELECT  a.edi_transaction_id, a.sender_id, a.receiver_id, a.gs_number, a.isa_number, a.file_name,            
        b.edi_997_detail_id, b.no_of_trans,
        c.edi_transaction_etail_id, c.po_number,                    
        d.edi_doc_type_id, d.doc_type

FROM    (((edi_transaction a 
        left join edi_997_details b on a.edi_transaction_id = b.edi_transaction_id)
        left join edi_transaction_details c on a.edi_transaction_id = c.edi_transaction_id)
        left join edi_doc_type d on a.edi_doc_type_id = d.edi_doc_type_id)

WHERE   a.file_name like '%Your file Name%'      
        and c.po_number like '%Your file Name%'    
        and d.doc_type = 'your doc type'    
        and a.flag = 1 AND c.flag = 1;

希望这能完成你的任务。