Oracle where clause

where clause from the table lookup or temporary data set specified condition is recorded, may be used to condition select, update, and delete statements.

A generating test data

Super Girl create basic information table (T_GIRL) with the following SQL, insert some test data.

create table T_GIRL
(
  id        char(4)         not null,   -- 编号
  name      varchar2(10)    not null,   -- 姓名
  yz        varchar2(10)        null,   -- 颜值
  sc        varchar2(10)        null,   -- 身材
  weight    number(4,1)     not null,   -- 体重
  height    number(3)       not null,   -- 身高
  birthday  date            not null,   -- 出生时间
  memo      varchar2(1000)      null    -- 备注
);
insert into T_GIRL(id,name,yz,birthday,sc,weight,height,memo)
  values('0101','西施','漂亮',to_date('2000-01-01 01:12:35','yyyy-mm-dd hh24:mi:ss'),
         '火辣',48.5,170,'这是一个非常漂亮姑娘,老公是夫差,男朋友是范蠡。');
insert into T_GIRL(id,name,yz,birthday,sc,weight,height,memo)
  values('0102','貂禅','漂亮',to_date('1997-08-02 12:20:38','yyyy-mm-dd hh24:mi:ss'),
         '苗条',45.2,168,'王允真不是男人,干不过董卓就把美人往火坑里推,千古罪人啊。');
insert into T_GIRL(id,name,yz,birthday,sc,weight,height,memo)
  values('0103','妲已','漂亮',to_date('1998-03-03 10:50:33','yyyy-mm-dd hh24:mi:ss'),
         '火辣',53.6,172,'如果商真的因我而亡,你们男人做什么去了?');
insert into T_GIRL(id,name,yz,birthday,sc,weight,height,memo)
  values('0104','芙蓉姐姐','猪扒',to_date('1980-05-05 10:11:55','yyyy-mm-dd hh24:mi:ss'),
         '膘肥体壮',85.8,166,'如果不努力学习技术,将来就会娶个芙蓉姐姐,哼哼。');
insert into T_GIRL(id,name,yz,birthday,sc,weight,height,memo)
  values('0105','神密猫女',null,to_date('1989-12-08 12:10:35','yyyy-mm-dd hh24:mi:ss'),
         null,48.5,171,'不知道是什么人,她脸上有一个%符号,很神密。');

Two, where clause syntax

select 字段名1,字段名2,......字段名n from 表名 where 字段名 比较运算符 值;
select * from 表名 where 字段名 比较运算符 值;

Example:

select id,name,yz,sc,height from T_GIRL where id='0101';

Here Insert Picture Description

select id,name,yz,sc,height from T_GIRL where height=172;

Here Insert Picture Description

The above example shows the simplest usage where clause, then I will introduce Oracle's logical, and comparison operators to enhance the functionality where clause.

Third, the logical operators

Operators Remark
and Binary operator, left and right if both conditions are true, the true value is obtained.
or Binary operator, about as long as two conditions is true, the true value is obtained.
not Refers to a single operator, if the former condition is true, true is obtained, if the condition is false element, whereas if the former condition is false, then the result is true. Used with comparison operators in general not in, like, null.

Logical operators have precedence, but I do not want to introduce it in practical applications, with brackets to solve all of the priority issues.

Example:

select name,yz,sc,height from T_GIRL where yz='漂亮' and sc='火辣';

Here Insert Picture Description

select name,yz,sc,height from T_GIRL where yz='漂亮' and (sc='火辣' or sc='苗条');

Here Insert Picture Description

Not look at the results, but rarely use it.

Here Insert Picture Description

Fourth, comparison operators

Here Insert Picture Description

Attention to several issues:

1) comparison operator can be used and not together, but in practical applications, in, like, and is null and not very commonly used in conjunction with the use of comparison operators rarely combined with other not, feeling awkward.

2) When using like, with a percent sign% match multiple characters, underscore _ matches one character, but if we want to% and _ as an ordinary character, the method can be used escape character, use escape characters are as follows:

In the C language, the use of the backslash \ escape, in Oracle, with the escape keyword defines the escape character.

escape 'chr' defined escape character, when the escape character is placed before the wildcard, the wildcard character is interpreted as normal, for example:

select * from T_GIRL where memo like '%人%/%%' escape '/';

First, second and fourth% as a wildcard, the third ordinary characters%.

Here Insert Picture Description

Five, where clause Advanced Usage

Where clause condition may be a constant (fixed value) or expression may be a function return value or select statement result set.

If the comparison operator is in the condition value may be a plurality of rows of the result set, other comparison operators result set must be recorded in a single row.

1) the value of condition is an expression.

select name,yz,sc,height from T_GIRL where height>100+68;

Here Insert Picture Description

2) and is a function of the value of the condition expression.

select name,yz,sc,height from T_GIRL where birthday>sysdate-(30*365);

Here Insert Picture Description

3) the value of the condition is the result of a single line set

select empno,ename,job,sal from EMP
 where deptno=(select deptno from DEPT where dname='ACCOUNTING');

Here Insert Picture Description

If the value is a multi-line conditions, problems logically, will prompt an error.

Here Insert Picture Description

4) comparison of the value of the condition in the back support in multiple rows the result set.

Here Insert Picture Description

Sixth, the use and operation of the column where clause functions

In the where clause, the column can be used and calculation functions.

1, operation of the column

select id,name,height from T_GIRL where height-170>0;

Here Insert Picture Description

2, using the function of the column

select id,name,to_char(birthday,'yyyy-mm-dd hh24:mi:ss') from T_GIRL
 where to_char(birthday,'yyyy-mm-dd hh24:mi:ss')='1998-03-03 10:50:33';

Here Insert Picture Description

3, problems

SQL to a height above the first column of the operation, the second column SQL is used on the birthday to_char function Both versions have zero. Because in the where clause, if the function or operation of the column, SQL statements can not use the index (an index function except), low performance (using a table scan), the programmer must not commit such a stupid mistake.

This is the correct value or condition calculates using the function, as follows:

select id,name,height from T_GIRL where height>170;
select id,name,to_char(birthday,'yyyy-mm-dd hh24:mi:ss') from T_GIRL
 where birthday=to_date('1998-03-03 10:50:33','yyyy-mm-dd hh24:mi:ss');

Side note, select the column name after the operation and use of keywords function has no effect on the performance of SQL statements.

Seven, copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published an original article · won praise 2 · Views 6722

Guess you like

Origin blog.csdn.net/u010806950/article/details/105058222