如果可以在MySQL中执行数学运算,那么可以进行ELSE吗?
为了执行数学运算和处理条件,可以考虑使用CASE语句。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FruitName varchar(100), FruitPrice int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(FruitName,FruitPrice) values('Orange',250);
mysql> insert into DemoTable(FruitName,FruitPrice) values('Banana',100);
mysql> insert into DemoTable(FruitName,FruitPrice) values('Apple',150);
mysql> insert into DemoTable(FruitName,FruitPrice) values('Pomegranate',200);使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-------------+------------+ | Id | FruitName | FruitPrice | +----+-------------+------------+ | 1 | Orange | 250 | | 2 | Banana | 100 | | 3 | Apple | 150 | | 4 | Pomegranate | 200 | +----+-------------+------------+ 4 rows in set (0.19 sec)
以下是对带有数学运算的CASE语句的查询-
mysql> select Id,FruitName,FruitPrice, case when FruitName='Orange' then FruitPrice/5 else FruitPrice end as OriginalPrice from DemoTable;
这将产生以下输出-
+----+-------------+------------+---------------+ | Id | FruitName | FruitPrice | OriginalPrice | +----+-------------+------------+---------------+ | 1 | Orange | 250 | 50.0000 | | 2 | Banana | 100 | 100 | | 3 | Apple | 150 | 150 | | 4 | Pomegranate | 200 | 200 | +----+-------------+------------+---------------+ 4 rows in set (0.00 sec)
热门推荐
1 狗狗下楼训练
3 狗狗训练头盔
6 狗狗训练机构
7 训练狗狗步骤
9 狗狗能训练
10 狗狗基本动作训练方法
11 怎么训练狗狗叼饭盆
12 狗狗一岁了还能训练吗
13 训练狗狗的
14 宁波狗狗训练
15 训练狗狗拉屎
16 教狗狗训练
17 狗狗要怎么训练上厕所
18 狗狗靠的训练