MySQL
 Computer >> コンピューター >  >> プログラミング >> MySQL

MySQLでテーブル内の2つの列を乗算した結果を合計して取得する方法

MySQLでは、集計関数 SUM() を使うことで、2つの列を掛け合わせた結果(積)を合計し、1つの値として取得できます。例えば「数量 × 単価 = 売上金額」のように、行ごとの計算結果を商品ごとに集計したい場合に非常に便利です。

準備:サンプルテーブルを作成する

まず、動作確認用のテーブルを作成しましょう。

mysql> create table DemoTable
    (
    CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    CustomerProductName varchar(100),
    CustomerProductQuantity int,
    CustomerPrice int
    );
Query OK, 0 rows affected (0.17 sec)

このテーブルは、顧客ID・商品名・数量・価格の4つの列を持つシンプルな構成です。

サンプルデータを挿入する

次に、INSERT文を使ってテストデータを登録します。

mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-1',5,400);
Query OK, 1 row affected (0.10 sec)

mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-2',3,100);
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-1',2,300);
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-1',5,50);
Query OK, 1 row affected (0.08 sec)

mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-3',6,10);
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-2',10,20);
Query OK, 1 row affected (0.03 sec)

登録したデータを確認する

SELECT文ですべてのレコードを表示してみましょう。

mysql> select *from DemoTable;

実行すると、以下のような結果が得られます。

+------------+---------------------+-------------------------+---------------+
| CustomerId | CustomerProductName | CustomerProductQuantity | CustomerPrice |
+------------+---------------------+-------------------------+---------------+
| 1          | Product-1           | 5                       | 400           |
| 2          | Product-2           | 3                       | 100           |
| 3          | Product-1           | 2                       | 300           |
| 4          | CustomerProductQuantity | 5                   | 50            |
| 5          | Product-3           | 6                       | 10            |
| 6          | Product-2           | 10                      | 20            |
+------------+---------------------+-------------------------+---------------+
6 rows in set (0.00 sec)

2つの列を乗算して合計するクエリ

ここが本題です。CustomerProductQuantity * CustomerPrice のように列同士を乗算し、その結果を SUM() で合計します。さらに GROUP BY を組み合わせることで、商品名ごとの合計金額を求められます。

mysql> select CustomerProductName,
SUM(CustomerProductQuantity*CustomerPrice) AS TOTAL_PRICE
from DemoTable
group by CustomerProductName;

実行結果は以下の通りです。

+---------------------+-------------+
| CustomerProductName | TOTAL_PRICE |
+---------------------+-------------+
| Product-1           | 2850        |
| Product-2           | 500         |
| Product-3           | 60          |
+---------------------+-------------+
3 rows in set (0.00 sec)

クエリの解説

このクエリのポイントは以下の3点です。

  • 列の乗算: CustomerProductQuantity * CustomerPrice により、各行ごとに「数量 × 単価」が計算されます。
  • SUM()による合計: 集計関数 SUM() が、乗算されたすべての行の値を足し合わせます。
  • GROUP BYによるグループ化: GROUP BY CustomerProductName により、同じ商品名のレコードがまとめられ、商品ごとの合計金額が算出されます。

検証してみると、Product-1の場合は「5×400 + 2×300 + 5×50 = 2000 + 600 + 250 = 2850」となり、正しく合計されていることがわかります。

応用:AS句で別名を付ける

AS TOTAL_PRICE のようにエイリアス(別名)を指定すると、出力結果の列名がわかりやすくなります。日本語のシステムでは AS 合計金額 のようにすることも可能です。

このように、SUM()GROUP BY を組み合わせれば、売上集計や在庫金額の算出など、実務でよくある要件に柔軟に対応できます。

  1. MySQLでUNIONを使って2つの列から重複しない値を取得する方法

    ```html MySQLで2つの列から一意な値を取得するには?2つの列(テーブル)から重複を除いた値を取得したい場合は、UNION演算子を使用します。UNIONは複数のSELECT文の結果を結合する際に、自動的に重複行を取り除いてくれるため、まさにこの用途に最適です。ここでは、実際にサンプルテーブルを作成しながら、具体的な手順を確認していきましょう。ステップ1:1つ目のテーブルを作成するまず、以下のCREATE TABLE文でサンプルテーブル「DemoTable1」を作成します。mysql> create table DemoTable1(   Value1 int

  2. 【MySQL】テーブルに新しい列を追加し、同じテーブルの他の2つの列から値を計算して入力する方法

    MySQLで新しい列を追加し、既存の2つの列から計算した値を入力する方法 この記事では、MySQLのテーブルに新しい列を追加し、同じテーブル内の他の2つの列のデータをもとに値を入力する方法を、実際の例を交えて解説します。 1. テーブルを作成する まず、サンプル用のテーブルを作成しましょう。 mysql> create table DemoTable ( Price int, Quantity int ); Query OK, 0 rows affected (0.71 sec) 2. レコードを挿入する INSERTコマンドを使用して、テーブルにいくつかのレコードを追加