sql - Fill in missing values in a SELECT statement -


i have table 2 columns, customer id , order. let's have in total order ids 1,2,3,4

all customer can have 4 orders, below:

    1234 1     1234 2     1234 3     1234 4     3245 3     3245 4     5436 2     5436 4 

you can see above 3245 customer doesn't have order id 1 or 2. how print in query output like:

3245 1 3245 2 5436 1 5436 3 

edit: don't have order table, have list of order's can hard code in query(1,2,3,4).

select  c.id, o.order    (         select  1 order         union         select  2 order         union         select  3 order         union         select  4 order         ) o cross join         (         select  distinct id            customer_orders         ) c   not exists         (          select  null            customer_orders ci           ci.id = c.id                 , ci.order = o.order         ) 

if have customers table, becomes more simple:

select  c.id, o.order    (         select  1 order         union         select  2 order         union         select  3 order         union         select  4 order         ) o cross join         customers c   not exists         (          select  null            customer_orders ci           ci.id = c.id                 , ci.order = o.order         ) 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -