Don E.
Don E.

Reputation: 83

order by from 2 column from different tables

I am trying to pull all the items for sales on a POS system.

I have a few tables I need to join and order/group by. I hope I can get this explained.

Table `items`:
+---------+--------+
| item_id |  Name  |
+---------+--------+
|    33   | Thing1 |
|    34   | Thing2 |
|    54   | Thing3 |
|    67   | Thing4 |
+---------+--------+

Table `kits`:
+-------------+------+
| item_kit_id | Name |
+-------------+------+
|      1      | Kit1 |
|      2      | Kit2 |
+-------------+------+


Table `sales`: 
+---------+-------------+---------+
| sale_id | sale_date   |  Total  |
+---------+-------------+---------+
|       1 |  2016-06-11 | 100.00  |
|       2 |  2016-06-12 | 145.00  |
+---------+-------------+---------+

Table `sale_items`:
+-----+---------+----------+---------+
| id  | sale_id | line_num | Item_id |
+-----+---------+----------+---------+
|  1  |     1   |    1     |    33   |
|  2  |     1   |    3     |    54   |
|  3  |     2   |    1     |    34   |
|  4  |     2   |    2     |    67   |
+-----+---------+----------+---------+

Table `sale_kit_items`:
+-----+---------+----------+-------------+
| id  | sale_id | line_num | item_kit_id |
+-----+---------+----------+-------------+
|  1  |     1   |    2     |       1     |
|  2  |     2   |    3     |       2     |
+-----+---------+----------+-------------+

I want to results to look like

Results:
+---------+----------+---------+-------------+
| sale_id | line_num | item_id | item_kit_id |
+---------+----------+---------+-------------+
|    1    |     1    |    33   |     null    |
|    1    |     2    |   null  |       1     |
|    1    |     3    |    54   |     null    |
|    2    |     1    |    54   |     null    |
|    2    |     2    |    67   |     null    |
|    2    |     3    |  null   |       2     |
+---------+----------+---------+-------------+

or even better if I could get:

Results:
+---------+----------+---------+-------------+--------+
| sale_id | line_num | item_id | item_kit_id |  name  |
+---------+----------+---------+-------------+--------+
|    1    |     1    |    33   |     null    | Thing1 |
|    1    |     2    |   null  |       1     |  Kit1  |
|    1    |     3    |    54   |     null    | Thing2 |
|    2    |     1    |    54   |     null    | Thing3 |
|    2    |     2    |    67   |     null    | Thing4 |
|    2    |     3    |  null   |       2     |  Kit2  |
+---------+----------+---------+-------------+--------+

Upvotes: 1

Views: 36

Answers (1)

Giorgos Altanis
Giorgos Altanis

Reputation: 2760

Took me sometime to understand your scenario...

EDIT

Perhaps you should change the LEFT JOINs with items and sale_kit_items to INNER JOINs (I expect that the IDs will be found in the respective tables).

select s.sale_id, si.line_num, si.item_id, null as item_kit_id, i.name
from sales s 
left join sale_items si on s.sale_id = si.sale_id
left join items i on si.item_id = i.item_id
union all
select s.sale_id, ski.line_num, null as item_id, ski.item_kit_id, k.name
from sales s 
left join sale_kit_items ski on s.sale_id = ski.sale_id 
left join kits k on ski.item_kit_id = k.item_kit_id
order by 1, 2
;

A live demo can be found here.

Upvotes: 1

Related Questions