使用してSQLクエリは、数量の合計を見つけるために参加します

Omkar Ghurye:

Productテーブル

billtranテーブル

ここでは、billtranテーブルでのProductID = 1を2回繰り返します

私が検索したいsum(billtran.quantity)個別のProductIDのを

私は、この出力、うちは私の私を期待されたいです

Query1をを選択します。

 select name,billtran.quantity from product inner join billtran on product.id=billtran.productid where product.id in(select id from product)

選択Query1を出力

キーズJard:

コメントあたり:

select name, SUM(billtran.quantity) from product inner join billtran on product.id=billtran.productid where product.id in(select id from product)
GROUP BY NAME

キャップの私の追加

注:where product.id in(select id from product)完全に役に立たないwhere句で、削除する必要があります。クエリは、where句を持っている必要はありませんし、テーブルのidは(常に真)、そのテーブル内のすべてのIDであるべきと言うものを必要としません

私は、それゆえ、この1のように書かれています:

SELECT
  p.name,
  SUM(b.quantity) as sumof_quantity
FROM 
  product p
  INNER JOIN billtran b ON p.id=b.productid 
GROUP BY 
  p.name

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=402687&siteId=1