Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

SQL Select Problem

  • 28-09-2010 08:58AM
    #1
    Registered Users, Registered Users 2 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, Paid Member Posts: 44,296 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

    Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/ .



  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Right join or left join orders to the customer table?


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


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


Advertisement