MySQL database learning (seven) - fuzzy query wildcard like 'between and' in 'is null' security equal

MySQL learn column is continuously updated in :)


Previous MySQL database learning (six) - Conditions query expression logical expression speak the query conditions, it is more precise but rigid, fuzzy queries here to talk about here.

like

This execution is a wildcard (wildcard character) function screening fills
us first try the following code:

USE data1;
SELECT 
`last_name`,	
  CONCAT(
    ',',`first_name`,
    ',',
    IFNULL(`manager_id`, 0),
    ',',
    IFNULL(`job_id`, 0),
    ',',
    IFNULL(`email`, 0),
    ',',
    IFNULL(`commission_pct`, 0)
  ) 职工基本情况,
  `salary` 
FROM
  employees

WHERE
  `last_name` LIKE '%b%';

% Is a wildcard , attention wildcards and regular expression (regular expression) is different, not the same purpose.
Wildcards are screening individuals (such as string or an attribute of an object) according to the situation,
the regular expression is the removal of fragments of what you want in an individual (a string), the removal process must first match (match ) , lock their part want, a chance.

Here are the results: Here Insert Picture Description
another logical expression before learning can be applied on, after all, whether it is fuzzy query or query conditions, are the expression :
the WHERE place of changing the code is as follows:

WHERE
  NOT(salary>=10000 AND salary<=20000)
  AND `last_name` LIKE '%b%';

The results are as follows:
Here Insert Picture Description
Now we look at the specific
LIKE support of the wildcard% _ introduced
in fact supports two
% sign on behalf of (wildcard) ** any number (including zero) of characters,% means that can represent all things ** , in addition to NULL , similar to a regular expression *, the asterisk matches we SELECT * FROM employees;also used.

Underscore _ on behalf of (wildcard) a character, similar to a regular expression ?.

Here's an chestnuts: To last_name second character is the second letter is underlined and first_name b owner information

WHERE
  `last_name` LIKE '_\_%'
  AND `first_name` LIKE '_b%';

Here Insert Picture Description
Here escaped the same way as the vast majority of language: the use of a backslash \
course, MySQL can also customize the escape character: for example, would like to change$

WHERE
  `last_name` LIKE '_$_%' ESCAPE '$'
  AND `first_name` LIKE '_b%';

between and

On a tutorial mentioned this, between and in fact, this function can be fully realized.
Here Insert Picture Description

WHERE
  salary between 10000 and 20000;

Here Insert Picture Description
Also replaced

WHERE
  salary between 20000 and 10000;

You can try okay :)

in

Here, we assume that the need to find all last_name as Peter, Christopher, David, Oliver, Patrick of people, how should you do?
On a knowledge that we would like this:

USE data1;
SELECT 
`last_name`,
`first_name`,
`job_id`,	
  CONCAT(
    `last_name`,
    ',',`first_name`,
    ',',
    IFNULL(`manager_id`, 0),
    ',',
    IFNULL(`job_id`, 0),
    ',',
    IFNULL(`email`, 0),
    ',',
    IFNULL(`commission_pct`, 0)
  ) 职工基本情况,
  `salary` 
FROM
  employees

WHERE
  first_name = 'Peter' OR
  first_name = 'Christopher' OR 
  first_name = 'David' OR 
  first_name = 'Oliver' OR 
  first_name = 'Patrick';

But it is clear where the part is too redundant code in a row if possible even worse,
and in that to solve this problem, we look at where the improvements:

WHERE
  first_name IN('Peter','Christopher','David','Oliver','Patrick');

is null 和 is not null

When the cell is empty, the value is NULL, we want to be judged against such a null value, how to do?

WHERE
  `commission_pct` = NULL;

That right? You can try okay 2333

WHERE
  `commission_pct` IS NULL;

Results are as follows:
Here Insert Picture Description
if not empty very simple

WHERE
  `commission_pct` IS NOT NULL;

Security equal to <=>

Above ISthe keyword NULL can only judge
and =can only judge the value of the ordinary (not NULL)
has a versatile player able to get all possible, that this is safe judgment :

WHERE
  `commission_pct` <=> NULL;

Here Insert Picture Description

WHERE
  `commission_pct` <=> 0.4;

Here Insert Picture Description
But conventional wisdom that the Security equal to the readability is not high, I think it very tricky 2333

Released eight original articles · won praise 7 · views 196

Guess you like

Origin blog.csdn.net/weixin_43178828/article/details/104075516