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

MySQL CASE式で「OR」条件を使用するにはどうすればよいですか?


MySQLCASE式に「OR」のような同じ条件を設定します。まず、サンプルテーブルを作成しましょう。

以下はクエリです

mysql> create table caseOrConditionDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(100),
   -> Score int
   -> );
Query OK, 0 rows affected (0.49 sec)
>

以下は、挿入コマンドを使用してテーブルにいくつかのレコードを挿入するためのクエリです。

mysql> insert into caseOrConditionDemo(Name,Score) values('Larry',85);
Query OK, 1 row affected (0.18 sec)

mysql> insert into caseOrConditionDemo(Name,Score) values('Sam',74);
Query OK, 1 row affected (0.20 sec)

mysql> insert into caseOrConditionDemo(Name,Score) values('Mike',76);
Query OK, 1 row affected (0.16 sec)

mysql> insert into caseOrConditionDemo(Name,Score) values('Carol',65);
Query OK, 1 row affected (0.20 sec)

以下は、selectコマンドを使用してテーブルのレコードを表示するためのクエリです。

mysql> select *from caseOrConditionDemo;

これにより、次の出力が生成されます

+----+-------+-------+
| Id | Name  | Score |
+----+-------+-------+
| 1  | Larry | 85    |
| 2  | Sam   | 74    |
| 3  | Mike  | 76    |
| 4  | Carol | 65    |
+----+-------+-------+
4 rows in set (0.00 sec)

以下は、MySQLCASE式で「OR」などの条件を使用するためのクエリです。

mysql> select Id,Name,Score,
   -> case when Score > 75 then 'Better Score'
   -> when Score > 70 then 'Good Score'
   -> else 'Not Good Score'
   -> end as 'Performance'
   -> from caseOrConditionDemo;

これにより、次の出力が生成されます

+----+-------+-------+----------------+
| Id | Name  | Score | Performance    |
+----+-------+-------+----------------+
| 1  | Larry | 85    | Better Score   |
| 2  | Sam   | 74    | Good Score     |
| 3  | Mike  | 76    | Better Score   |
| 4  | Carol | 65    | Not Good Score |
+----+-------+-------+----------------+
4 rows in set (0.04 sec)

  1. MySQLクエリでCASE条件でカウントを使用するにはどうすればよいですか?

    MySQLでこれにCASEWHENを使用し、COUNT()メソッド内でCASE条件を設定してカウントします。まずテーブルを作成しましょう- mysql> create table DemoTable1374    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20),    -> Score int    -> ); Query OK, 0 rows affe

  2. MySQLのselectでif/else条件を使用するにはどうすればよいですか?

    まずテーブルを作成しましょう- mysql> create table DemoTable1966    (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(20),    PhotoLiked int    ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert int