mysql - sql join two tables, one summary the other is detail -
when join 2 tables, not sure how join following tables exact wanted.
table a:
-------------------------------------- | id | name | buy time | total | -------------------------------------- | 1 | | 3 | 30 | -------------------------------------- | 2 | b | 1 | 10 | --------------------------------------
table b:
------------------------------- | id | orderid | price | ------------------------------- | 1 | 1 | 10 | ------------------------------- | 1 | 2 | 10 | ------------------------------- | 1 | 3 | 10 | ------------------------------- | 2 | 4 | 10 | -------------------------------
join table c
--------------------------------------------------------- | id | name | buy time | total | orderid | price | --------------------------------------------------------- | 1 | | 3 | 30 | | | --------------------------------------------------------- | 1 | | | | 1 | 10 | --------------------------------------------------------- | 1 | | | | 2 | 10 | --------------------------------------------------------- | 1 | | | | 3 | 10 | --------------------------------------------------------- | 2 | b | 1 | 10 | | | --------------------------------------------------------- | 2 | | | | 4 | 10 | ---------------------------------------------------------
if use "left out join on a.id = b.id" blank area filled duplicated value, cause sum of total income incorrect
another way "select 0 name b", fill blank 0, disaster if number of column many.
therefore, ask there better way achieve goal?
what want union
of both tables:
select id, name, `buy time`, total, null orderid, null price union select id, null, null, null, orderid, price b order id, name desc, orderid
Comments
Post a Comment