MySQL (four) DQL language - condition inquiry

Abstract: Conditions inquiry; conditional expressions, logical expressions, fuzzy query, like, a wildcard escape character, escape key, between and, in, is null, is not null, secure equal.

Criteria query
syntax:

SELECT
query list
FROM
table
WHERE
filter criteria;
1
2
3
4
5
6
we conduct some simple filter conditions according to the classification:

Conditional Expression Filter
conditional operator:
>: greater than
<: less than
=: equal to
= or <!>: Is not equal to
> =: greater than or equal to
<=: less than or equal to

Example
query salary is greater than 12,000 employee information.
SELECT * FROM employees WHERE salary> 12000 ;

Queries department number is not equal to number of 90 employees name and department number.
The SELECT
FIRST_NAME,
DEPARTMENT_ID
the FROM
the Employees
the WHERE
DEPARTMENT_ID <> 90;
. 1
2
. 3
. 4
. 5
. 6
. 7

Filter Press logical expression
logical operators:
logic operator is connected to the conditional expression.
&& or and: with
|| or or: or
or not:! Non -

Example
query wage employee name, salary and bonus rate of between 10,000 to 20,000.
The SELECT
FIRST_NAME,
the salary,
a commission_pct
the FROM
the Employees
the WHERE
the salary> 10000 && the salary <20000;
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8

Queries department number is not between 90-110, employee information or salary higher than 15,000.
The SELECT
*
the FROM
the Employees
the WHERE
the NOT (DEPARTMENT_ID <= 110 the AND DEPARTMENT_ID> = 90) OR the salary> 15000;
. 1
2
. 3
. 4
. 5
. 6

Fuzzy query
fuzzy query is also a conditional expression, can be seen as a complex conditional expressions.
like
the BETWEEN and
in
IS null or is not null

We introduce one by one:

1) like
like: the meaning is "like."

General and with the use of wildcards.
Wildcards:
%: arbitrary string containing characters 0.
_: Any single character

If the character we want to use is the "%" or "_" because they themselves are wildcards, so we want to use them as a character, we need an escape character:
\ _: Underline
\%: Percent
of course in front of the escape character "\" is not a mandatory requirement, we can also "eSCAPE" to customize the escape character with the keyword. This can be seen in our third example below it.

like usage examples
we cite an example, the query employee information employee name contains characters of "a".
The SELECT
*
the FROM
the Employees
the WHERE
FIRST_NAME the LIKE '% A%';
. 1
2
. 3
. 4
. 5
. 6

We give another example, the query employee names third character is "e", the fifth character is "a" of employee names and salaries of employees.
The SELECT
FIRST_NAME,
the salary
the FROM
the Employees
the WHERE FIRST_NAME the LIKE '__e_a%';
. 1
2
. 3
. 4
. 5
. 6

Employee names and salaries third example, a query of employees ** name the second character is an underscore "_" employees. ** Here we need to use the escape character described above.
The SELECT
last_name,
the salary
the FROM
the Employees
the WHERE
last_name the LIKE '_ \ _%';
. 1
2
. 3
. 4
. 5
. 6
. 7

In some special cases, we can also customize the escape character, use the ESCAPE keyword. Examples of the above, we can write:
the SELECT
last_name,
the salary
the FROM
the Employees
the WHERE
last_name the LIKE '_ $ _%' the ESCAPE '$';
. 1
2
. 3
. 4
. 5
. 6
. 7
so that we define as our $ escape character. We can see the effect is the same.

2) the BETWEEN and
the BETWEEN and: means "between what."

We give an example: query employee information employee number between 100 and 120.
We usually write conditions here employee_id> = 100 AND employee_id <= 120.
We can also use between and, between and above the sentence and sentence are equivalent, so that we can write

The SELECT
*
the FROM
the Employees
the WHERE
the employee_id the BETWEEN 100 the AND 120;
. 1
2
. 3
. 4
. 5
. 6
the effect is the same can be achieved:

But we should pay attention to several points:

Use between and can greatly improve the conciseness sql statement;
comprising threshold value, for example, in the above example, 100 and 120 comprising the
front and back reversed order not! This is important, if written between 120 and 100, so not being given, but there will be no result.
3) in
in means "in ...... inside."

We are also to give an example: Query number of employees jobs are employees of any one of IT_PROG, AD_VP, AD_PRES in the number of employee names and jobs.
We can use the logical operators OR, can be written as:

= the job_id 'IT_PROG' the job_id = OR 'AD_VP' the job_id = OR 'AD_PRES';
. 1
We can also use the IN written as:

The SELECT
last_name,
the job_id
the FROM
the Employees
the WHERE the job_id the IN ( 'IT_PROG', 'AD_VP', 'AD_PRES');
. 1
2
. 3
. 4
. 5
. 6
we look at the results:

We introduce the IN features:

IN role is to determine whether the value of a field of an item in the list IN.
IN can improve the use of simple SQL statements.
IN list value types must be consistent or compatible.
In the IN list does not support wildcards. All characters will be handled as a character itself.
4) is null and null Not IS
IS null and is not null: determining whether a null value.

Let's give an example, we have to check what no prize money and bonuses of employees were employees.
At this time we want to use is null or is not null:

SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct IS NULL ;
1
2
3
4
5
6
7

So I ask that we can use commission_pct = NULL do?
The answer is no.

But also to note that IS NULL is a whole, IS NULL worth can only be used to judge, do not judge others.

Added: secure equal
Finally, we add SQL inside the "security equal to" symbol is "<=>"

Thus safety is based on a null value.
Analyzing of course also be used for normal value, and "=" the same.
But security equal with very little, because it is relatively poor readability. So it is recommended to use ordinary "=" or "IS NULL".

Such as the above example to determine whether the bonus is null, we can write the conditional expression:

The SELECT
last_name,
a commission_pct
the FROM
the Employees
the WHERE
a commission_pct <=> NULL;
. 1
2
. 3
. 4
. 5
. 6
. 7
above determination is an example of a query wages of 12,000, can be written as

SELECT
*
FROM
employees
WHERE
salary <=> 12000 ;
1
2
3
4
5
6

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

Guess you like

Origin www.cnblogs.com/hyhy904/p/10987455.html