网站建设资讯

NEWS

网站建设资讯

oracle怎么使用if oracle教程 菜鸟教程

orcale语句如何用if判断将一个数据的长度大于某个值的一部分数据另存为一个新?

在oracle中,我们可以用case when 代替if

成都创新互联是专业的加查网站建设公司,加查接单;提供成都网站制作、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行加查网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

case when length(id)7 then '成功' else '失败' end name (没办法把整个语句发上来,一发就说网络异常)

如果另建新表(这张表需要确实存在),那么就create table table_name后面加上上面的语句就可以了。

如果你的name字段已经存在,也就是说你需要在那么中加上成功个失败的字样,那么就需要稍微修改一下

case when length(id)7 then '成功' else '失败' end name

改为

case when length(id)7 then name||'成功' else name||'失败' end name

具体的要根据实际需求酌情修改

oracleif判断语句

oracle的if语句采用decode函数。

DECODE(value,if1,then1,if2,then2,if3,then3,...,else)

表示如果value 等于if1时,DECODE函数的结果返回then1,...,如果不等于任何一个if值,则返回else。

Oracle数据库是对标准sql语言的过程化扩展,因此产生了pl/sql语言。其中的if语句大量使用使得程序模块化的功能方便实用。现在要讨论的是if语句的基本使用方法。

连接数据库

请输入用户名:  scott/123456

设置环境变量

SQL set serveroutput on

定义两个字符串变量,然后赋值,接着使用if……then语句比较两个字符串变量的长度,并输出比较结果。

declare

a varchar(10);

b varchar(10);

begin

a:='beijing';

b:='guangdong';

if length(a)length(b)

then dbms_output.put_line('ab');

end if;

end;

过if……then……else语句实现只有年龄大于等于56岁,才可以申请退休,否则程序会提示不可以申请退休。

declare

a number(10);

begin

a:=x;

if a=56

then dbms_output.put_line('可以申请退休');

else dbms_output.put_line('不可以申请退休');

end if;

end;

制定一个月份数值,然后使用if……then……elsif语句判断它所属的季节,并输出季节信息。

declare

mon number(10);

begin

mon:=x;

if mon=3 or mon=4 or mon=5

then dbms_output.put_line('春节');

elsif mon=6 or mon=7 or mon=8 then dbms_output.put_line('夏季');

elsif mon=9 or mon=10 or mon=11 then dbms_output.put_line('秋季');

elsif mon=12 or mon=1 or mon=2 then dbms_output.put_line('冬季');

end if;

end;

制定一个季度数值,然后使用case语句判断它所包含的月份信息并输出。

declare

ss number(10);

begin

ss:=x;

case

when ss=1 then dbms_output.put_line('包含月份3,4,5');

when ss=2 then dbms_output.put_line('包含月份6,7,8');

when ss=3 then dbms_output.put_line('包含月份9,10,11');

when ss=4 then dbms_output.put_line('包含月份12,1,2');

end case;

end;

求助!!oracle if语句的使用

你这个应该把count(A)赋给一个变量 count(B)也同样,然后比较两个变量

比如:select count(A) into 变量1 ,count(B) into 变量2 from aaaa;

if 变量1变量2 then

选择变量1对应的行

else

选择变量2对应的行

endif

在oracle sql语句里有没有if...else...的用法,请各位大侠给个例子看看,灰常感谢!!

oracle 中if ..else 可以再pl/sql 中使用,

如果是要在SQL语句中达到这种效果可以用case when ... then ...else ..end;

mysql数据库中CASE WHEN语句。

case when语句,用于计算条件列表并返回多个可能结果表达式之一。

CASE 具有两种格式:

简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。

CASE 搜索函数计算一组布尔表达式以确定结果。

两种格式都支持可选的 ELSE 参数。

语法

简单 CASE 函数:

复制代码 代码如下:

CASE input_expression

WHEN when_expression THEN result_expression

[ ...n ]

[

ELSE else_result_expression

END

CASE 搜索函数:

复制代码 代码如下:

CASE

WHEN Boolean_expression THEN result_expression

[ ...n ]

[

ELSE else_result_expression

END

参数

input_expression

是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft? SQL Server? 表达式。

WHEN when_expression

使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL

Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。

占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。

THEN result_expression

当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。

result expression 是任意有效的 SQL Server 表达式。

ELSE else_result_expression

当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL

值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression

和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。

WHEN Boolean_expression

使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。

结果类型

从 result_expressions 和可选 else_result_expression 的类型集合中返回最高的优先规则类型。有关更多信息,请参见数据类型的优先顺序。

结果值

简单 CASE 函数:

计算 input_expression,然后按指定顺序对每个 WHEN 子句的 input_expression = when_expression 进行计算。

返回第一个取值为 TRUE 的 (input_expression = when_expression) 的 result_expression。

如果没有取值为 TRUE 的 input_expression = when_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。

CASE 搜索函数:

按指定顺序为每个 WHEN 子句的 Boolean_expression 求值。

返回第一个取值为 TRUE 的 Boolean_expression 的 result_expression。

如果没有取值为 TRUE 的 Boolean_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。

下面分享一些mysql case when语句的例子。

A. 使用带有简单 CASE 函数的 SELECT 语句

在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。

例子,使用 CASE 函数更改图书分类显示。

复制代码 代码如下:

USE pubs

GO

SELECT Category =

CASE type

WHEN 'popular_comp' THEN 'Popular Computing'

WHEN 'mod_cook' THEN 'Modern Cooking'

WHEN 'business' THEN 'Business'

WHEN 'psychology' THEN 'Psychology'

WHEN 'trad_cook' THEN 'Traditional Cooking'

ELSE 'Not yet categorized'

END,

CAST(title AS varchar(25)) AS 'Shortened Title',

price AS Price

FROM titles

WHERE price IS NOT NULL

ORDER BY type, price

COMPUTE AVG(price) BY type

GO

注释,后来我试了一下不让用category=。

我使用的代码为:

复制代码 代码如下:

SELECT

case gender

WHEN 1 THEN 'NAN'

WHEN 0 THEN 'NV'

end as gender

FROM

t_swidy_day_nutrient

结果集:

Category Shortened Title Price

------------------- ------------------------- --------------------------

Business You Can Combat Computer S 2.99

Business Cooking with Computers: S 11.95

Business The Busy Executive's Data 19.99

Business Straight Talk About Compu 19.99

avg

==========================

13.73

Category Shortened Title Price

------------------- ------------------------- --------------------------

Modern Cooking The Gourmet Microwave 2.99

Modern Cooking Silicon Valley Gastronomi 19.99

avg

==========================

11.49

Category Shortened Title Price

------------------- ------------------------- --------------------------

Popular Computing Secrets of Silicon Valley 20.00

Popular Computing But Is It User Friendly? 22.95

avg

==========================

21.48

Category Shortened Title Price

------------------- ------------------------- --------------------------

Psychology Life Without Fear 7.00

Psychology Emotional Security: A New 7.99

Psychology Is Anger the Enemy? 10.95

Psychology Prolonged Data Deprivatio 19.99

Psychology Computer Phobic AND Non-P 21.59

avg

==========================

13.50

Category Shortened Title Price

------------------- ------------------------- --------------------------

Traditional Cooking Fifty Years in Buckingham 11.95

Traditional Cooking Sushi, Anyone? 14.99

Traditional Cooking Onions, Leeks, and Garlic 20.95

avg

==========================

15.96

(21 row(s) affected)

B. 使用带有简单 CASE 函数和 CASE 搜索函数的

SELECT 语句

在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换。

例子:根据图书的价格范围将价格(money 列)显示为文本注释。

复制代码 代码如下:

USE pubs

GO

SELECT 'Price Category' =

CASE

WHEN price IS NULL THEN 'Not yet priced'

WHEN price 10 THEN 'Very Reasonable Title'

WHEN price = 10 and price 20 THEN 'Coffee Table Title'

ELSE 'Expensive book!'

END,

CAST(title AS varchar(20)) AS 'Shortened Title'

FROM titles

ORDER BY price

GO

结果集:

Price Category Shortened Title

--------------------- --------------------

Not yet priced Net Etiquette

Not yet priced The Psychology of Co

Very Reasonable Title The Gourmet Microwav

Very Reasonable Title You Can Combat Compu

Very Reasonable Title Life Without Fear

Very Reasonable Title Emotional Security:

Coffee Table Title Is Anger the Enemy?

Coffee Table Title Cooking with Compute

Coffee Table Title Fifty Years in Bucki

Coffee Table Title Sushi, Anyone?

Coffee Table Title Prolonged Data Depri

Coffee Table Title Silicon Valley Gastr

Coffee Table Title Straight Talk About

Coffee Table Title The Busy Executive's

Expensive book! Secrets of Silicon V

Expensive book! Onions, Leeks, and G

Expensive book! Computer Phobic And

Expensive book! But Is It User Frien

(18 row(s) affected)

C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数

例子,使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。

首先,来看下 CASE 的语法。在一般的 SELECT 中,其语法如下:

复制代码 代码如下:

SELECT myColumnSpec =

CASE

WHEN A THEN somethingA

WHEN B THEN somethingB

ELSE somethingE

END

以上代码,需要用具体的参数代替尖括号中的内容。

甚至还可以组合这些选项,添加一个 ORDER BY 子句,例如:

复制代码 代码如下:

USE pubs

GO

SELECT

CASE

WHEN price IS NULL THEN 'Unpriced'

WHEN price 10 THEN 'Bargain'

WHEN price BETWEEN 10 and 20 THEN 'Average'

ELSE 'Gift to impress relatives'

END AS Range,

Title

FROM titles

GROUP BY

CASE

WHEN price IS NULL THEN 'Unpriced'

WHEN price 10 THEN 'Bargain'

WHEN price BETWEEN 10 and 20 THEN 'Average'

ELSE 'Gift to impress relatives'

END,

Title

ORDER BY

CASE

WHEN price IS NULL THEN 'Unpriced'

WHEN price 10 THEN 'Bargain'

WHEN price BETWEEN 10 and 20 THEN 'Average'

ELSE 'Gift to impress relatives'

END,

Title

GO

除了选择自定义字段之外,在很多情况下 CASE 都非常有用。

稍加深入,还可以得到以前认为不可能得到的分组排序结果集。

使用CASE WHEN进行字符串替换处理

在SELECT查询中使用CASE WHEN

复制代码 代码如下:

/*

mysql SELECT Name, RatingID AS Rating,

- CASE RatingID

- WHEN 'R' THEN 'Under 17 requires an adult.'

- WHEN 'X' THEN 'No one 17 and under.'

- WHEN 'NR' THEN 'Use discretion when renting.'

- ELSE 'OK to rent to minors.'

- END AS Policy

- FROM DVDs

- ORDER BY Name;

+-----------+--------+------------------------------+

| Name | Rating | Policy |

+-----------+--------+------------------------------+

| Africa | PG | OK to rent to minors. |

| Amadeus | PG | OK to rent to minors. |

| Christmas | NR | Use discretion when renting. |

| Doc | G | OK to rent to minors. |

| Falcon | NR | Use discretion when renting. |

| Mash | R | Under 17 requires an adult. |

| Show | NR | Use discretion when renting. |

| View | NR | Use discretion when renting. |

+-----------+--------+------------------------------+

8 rows in set (0.01 sec)

*/

Drop table DVDs;

CREATE TABLE DVDs (

ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,

Name VARCHAR(60) NOT NULL,

NumDisks TINYINT NOT NULL DEFAULT 1,

RatingID VARCHAR(4) NOT NULL,

StatID CHAR(3) NOT NULL

)

ENGINE=INNODB;

INSERT INTO DVDs (Name, NumDisks, RatingID, StatID)

VALUES ('Christmas', 1, 'NR', 's1'),

('Doc', 1, 'G', 's2'),

('Africa', 1, 'PG', 's1'),

('Falcon', 1, 'NR', 's2'),

('Amadeus', 1, 'PG', 's2'),

('Show', 2, 'NR', 's2'),

('View', 1, 'NR', 's1'),

('Mash', 2, 'R', 's2');

SELECT Name, RatingID AS Rating,

CASE RatingID

WHEN 'R' THEN 'Under 17 requires an adult.'

WHEN 'X' THEN 'No one 17 and under.'

WHEN 'NR' THEN 'Use discretion when renting.'

ELSE 'OK to rent to minors.'

END AS Policy

FROM DVDs

ORDER BY Name;

oracle的update与if多个判断怎么用?

下边是我自己写的,但是执行起来报错,请前辈们解答,感谢~

update salary201911 set 个税 =

(

case when 计税金额 =36000 then 计税金额*3%-年度个税累计  when  36000计税金额 =144000 then 计税金额*10% - 2520 - 年度个税累计

when 144000计税金额 =300000 then 计税金额*20% - 16920 - 年度个税累计

when 300000计税金额 =420000 then 计税金额*25% - 31920 - 年度个税累计

when 420000计税金额 =660000 then 计税金额*30% - 52920 - 年度个税累计

when 660000计税金额 =960000 then 计税金额*35% - 85920 - 年度个税累计

else 计税金额*45% - 181920 - 年度个税累计

end

);


网页题目:oracle怎么使用if oracle教程 菜鸟教程
当前路径:http://cdweb.net/article/hhphdc.html