0

I have 2 tables (Product and Order) like this:

Product Table:

id name
1 A
2 B
3 C

Order Table:

id user_id product_id date quantity
1 1 1 2022-07-01 2
2 1 1 2022-07-02 3
3 1 2 2022-07-01 5
4 1 1 2022-07-02 2
5 1 1 2022-07-03 12
6 1 3 2022-07-05 2
7 1 2 2022-07-05 1

expect table

Name 2022-07-01 2022-07-02 2022-07-03 2022-07-04 2022-07-05
A 2 3 12 0 0
B 5 0 0 0 1
C 0 0 0 0 2

How to write a MySQL query to get the result like the 'expect table'

Jade
  • 1
  • 1
  • Use "pivot" as one of the keywords when searching. This is one of the most-asked SQL questions. – outis Jul 15 '22 at 01:48
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jul 15 '22 at 03:38
  • Since SQL includes data definition, a [mcve] for an [SQL question](//meta.stackoverflow.com/q/333952/90527) should include [DDL](//en.wikipedia.org/wiki/Data_definition_language) statements for sample tables (rather than an ad hoc table specification) and [DML](//en.wikipedia.org/wiki/Data_manipulation_language) statements for sample data (rather than a dump or ad hoc format). Desired results don't need to be presented as sample code, as results are the output of code and not code themselves. – outis Jul 15 '22 at 16:29
  • Does this answer your question? "[Mysql, reshape data from long / tall to wide](https://stackoverflow.com/q/2255640/90527)", "[How would you solve this tricky SQL problem?](https://stackoverflow.com/q/710941/90527)" – outis Jul 15 '22 at 16:33

0 Answers0