CASE WHEN 子句中的表达式为何在 MySQL 查询中不起作用?
要实现此目的,请在 MySQL 中正确使用 CASE WHEN 语句。让我们看看如何操作。
让我们创建一个表 −
mysql> create table demo58 −> ( −> id int not null auto_increment primary key, −> first_name varchar(20), −> last_name varchar(20) −> ); Query OK, 0 rows affected (2.15 sec)
使用插入命令向表中插入一些记录 −
mysql> insert into demo58(first_name,last_name) values('John','Doe');
Query OK, 1 row affected (0.12 sec)
mysql> insert into demo58(first_name,last_name) values('David','Smith');
Query OK, 1 row affected (0.29 sec)
mysql> insert into demo58(first_name,last_name) values('John','Brown');
Query OK, 1 row affected (0.11 sec)
mysql> insert into demo58(first_name,last_name) values('David','Miller');
Query OK, 1 row affected (0.26 sec)使用 select 语句显示表中的记录 −
mysql> select *from demo58;
将生成以下输出 −
+----+------------+-----------+ | id | first_name | last_name | +----+------------+-----------+ | 1 | John | Doe | | 2 | David | Smith | | 3 | John | Brown | | 4 | David | Miller | +----+------------+-----------+ 4 rows in set (0.15 sec)
以下是 CASE WHEN 中表达式的查询 −
mysql> select −> ( −> case −> when (first_name = "John") then 'Carol' −> when (last_name ="Smith") then 'Doe' −> end −> ) RESULT −> from demo58;
将生成以下输出 −
+--------+ | RESULT | +--------+ | Carol | | Doe | | Carol | | NULL | +--------+ 4 rows in set (0.00 sec)
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
安卓系统
Python
C 编程
C++
C #
MongoDB
MySQL
JavaScript
PHP