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

MySQLの1つのクエリで2つの異なる列を数えますか?


CASEステートメントを使用して、1つのクエリで2つの異なる列をカウントできます。概念を理解するために、最初にテーブルを作成しましょう。テーブルを作成するためのクエリは次のとおりです。

mysql> create table CountDifferentDemo
   - > (
   - > ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   - > ProductName varchar(20),
   - > ProductColor varchar(20),
   - > ProductDescription varchar(20)
   - > );
Query OK, 0 rows affected (1.06 sec)

挿入コマンドを使用して、テーブルにいくつかのレコードを挿入します。

クエリは次のとおりです

mysql> insert into CountDifferentDemo(ProductName,ProductColor,ProductDescription) values('Product-1','Red','Used');
Query OK, 1 row affected (0.46 sec)
mysql> insert into CountDifferentDemo(ProductName,ProductColor,ProductDescription) values('Product-1','Blue','Used');
Query OK, 1 row affected (0.17 sec)
mysql> insert into CountDifferentDemo(ProductName,ProductColor,ProductDescription) values('Product-2','Green','New');
Query OK, 1 row affected (0.12 sec)
mysql> insert into CountDifferentDemo(ProductName,ProductColor,ProductDescription) values('Product-2','Blue','New');
Query OK, 1 row affected (0.14 sec)
mysql> insert into CountDifferentDemo(ProductName,ProductColor,ProductDescription) values('Product-3','Green','New');
Query OK, 1 row affected (0.21 sec)
mysql> insert into CountDifferentDemo(ProductName,ProductColor,ProductDescription) values('Product-4','Blue','Used');
Query OK, 1 row affected (0.20 sec)

selectステートメントを使用してテーブルのすべてのレコードを表示します。

クエリは次のとおりです

mysql> select *from CountDifferentDemo;

以下は出力です

+-----------+-------------+--------------+--------------------+
| ProductId | ProductName | ProductColor | ProductDescription |
+-----------+-------------+--------------+--------------------+
|         1 | Product-1   | Red          | Used               |
|         2 | Product-1   | Blue         | Used               |
|         3 | Product-2   | Green        | New                |
|         4 | Product-2   | Blue         | New                |
|         5 | Product-3   | Green        | New                |
|         6 | Product-4   | Blue         | Used               |
+-----------+-------------+--------------+--------------------+
6 rows in set (0.01 sec)

これは、1つのクエリで2つの異なる列をカウントするクエリです。つまり、特定の色「赤」と説明「新規」の出現をカウントしています。

mysql> select ProductName,
   - > SUM(CASE WHEN ProductColor = 'Red' THEN 1 ELSE 0 END) AS Color,
   - > SUM(CASE WHEN ProductDescription = 'New' THEN 1 ELSE 0 END) AS Desciption
   - > from CountDifferentDemo
   - > group by ProductName;

以下は出力です

+-------------+-------+------------+
| ProductName | Color | Desciption |
+-------------+-------+------------+
| Product-1   |     1 |          0 |
| Product-2   |     0 |          2 |
| Product-3   |     0 |          1 |
| Product-4   |     0 |          0 |
+-------------+-------+------------+
4 rows in set (0.12 sec)

  1. MySQLの2つの異なる列を使用したカスタムソート?

    このためには、CASEステートメントとともにORDERBY句を使用します。まずテーブルを作成しましょう- mysql> create table DemoTable1610    -> (    -> Marks int,    -> Name varchar(20)    -> ) ; Query OK, 0 rows affected (0.51 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTa

  2. 1つのMySQLクエリで2つの列を更新します

    このためには、SETコマンドを1回だけ使用する必要があります。まずテーブルを作成しましょう- mysql> create table DemoTable1909    (    Id int NOT NULL,    FirstName varchar(20),    LastName varchar(20)    ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> ins