SQL Query to join 3 tables with the same FK -


Say I have 3 tables:

  1. Products (id, product number)
  2. Quotation (ID, Quotation Number, Product ID, Order Ed)
  3. Order (ID, Order Number, Product Id)

    For all quotations No order, all orders are not quoted.

    The output I want to achieve is:

      no product. No quotation. Currently I can get it in 1 using the combined 2 questions. Is there any way, I can only use 1 question?  
      
      Add SELECT Product.ProductNumber, Quotation.QuotationNumber, Order.OrderNumber Product. Order the product on the quotation. Id = quotation.ProductId LEFT JOIN Product.Id = Order.ProductId and Quotation.OrderId = Order.Id Union SELECT Product. ProductNumber, NULL, Order.OrderNumber Product Add Order Product.Id = Order. PRODUCTId LEFT JOIN QUOTATION at Product.Id = Quotation.ProductId and Order.Id = Quotation.OrderId where the Quotation Id is NULL   

    Update:

    1. If there is no quotation in the product and there is no order, then not shown.
    2. The quotation and order has the link specified in the quotation table.

      Example:

        Products: P1 P2 P3 P4 P5 Quotation: Q1 | P1 | O1Q2 | P2 | O2Q3 | P3. Finger Order: O1 | P1O2 P2O3 P3o4 P4o5 | Faucet Output: P1 | Question 1 | O1p2 Q2 | O 2p3 Q3 | NULL P3 | Faucet O3p4 Faucet O4   

      1:

      1. If the quotation and order are not linked, then questions 3 and 3 will be shown in line 1, which is incorrect is.
      2. But if they are connected, then only the first 2 rows will be shown.

        That's why I had to divide it into 2. But I'm just wondering if possible with 1.

        Try this:

          Product.ProductNumber, Quotation.QuotationNumber, Order product.Id = Quote on product left product from Order.OrderNumber. Leave excavation Order order on product order. Product = ID Product ID    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -