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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

SQL Select Problem

  • 28-09-2010 8: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 Posts: 41,235 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, 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