やりたいこと
以下の三つのテーブル連結してorders.taxonomyごとに商品の合計数を取り出したいです。
orders.taxonomy = 0
products.name | sum(quantity) | orders.taxonomy |
---|---|---|
商品A | 21 | 0 |
商品B | 9 | 0 |
商品C | 1 | 0 |
orders.taxonomy = 1
products.name | sum(quantity) | orders.taxonomy |
---|---|---|
商品A | 19 | 1 |
商品B | 20 | 1 |
商品C | 3 | 1 |
テーブル
productsテーブル
code | name |
---|---|
1 | 商品A |
2 | 商品B |
3 | 商品C |
ordersテーブル
code | date | taxonomy |
---|---|---|
1 | xxxx-xx-xx | 0 |
2 | xxxx-xx-xx | 1 |
3 | xxxx-xx-xx | 0 |
4 | xxxx-xx-xx | 0 |
order_productsテーブル
|code|code_orders|code_products|quantity
|:--|:--|:--|
|1|4|1|10|
|2|2|2|7|
|3|3|3|5|
|4|1|1|10|
|5|4|1|10|
|6|2|2|7|
|7|3|3|5|
|8|1|1|10|
困っていること
以下のように二回やることで取り出すことには成功しています。
二回接続するのはしょうがないのでしょうか?ご教授願います。
SQL
1SELECT products.name, SUM(order_products.quantity) 2FORM products, orders, order_products 3WHERE products.code = order_products.code_products 4AND orders.code = order_products.code_orders 5AND orders.taxonomy = 0 6 7SELECT products.name, SUM(order_products.quantity) 8FORM products, orders, order_products 9WHERE products.code = order_products.code_products 10AND orders.code = order_products.code_orders 11AND orders.taxonomy = 1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/10 10:44