Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

SQL Select Problem

Options
  • 28-09-2010 9:58am
    #1
    Registered Users Posts: 302 ✭✭


    I have an SQL issue (simplified) where i am selecting all orders and listing the order and customer details. What this does not show me is any customer that does not have an order. E.g.

    select o.*, c.*
    from order o
    left join customer as c on o.customer_id = c.id;

    Can this be rewritten so that it will include the customers that do not have orders. I know that I can independently determine what customers don't have orders and then do a union, but there are a lot of fields in the select statement and these are subject to change. The ideal solution for me is to be able to do it in one bite if this is possible.


Comments

  • Moderators, Politics Moderators Posts: 39,853 Mod ✭✭✭✭Seth Brundle


    The problem with a If you are planning on changing them then create two separate queries union-ised is that the oneshowing customers without orders won't have the same number of columns as the query above so the union won't work properly.

    Try a left join IMO as there shouldn't be any orders without a customer so the balance will stay on the customers side.
    http://www.w3schools.com/Sql/sql_join_left.asp


  • Registered Users Posts: 981 ✭✭✭fasty


    Right join or left join orders to the customer table?


  • Registered Users Posts: 3,721 ✭✭✭E39MSport


    SELECT *
    FROM
    CUSTOMER C
    LEFT OUTER JOIN
    ORDER O
    ON
    C.ID = O.CUSTOMER_ID


Advertisement