Skip to content

[MySQL] Note(3) IN, BETWEEN, LIKE, %, _

I briefly mentioned that the WHERE instruction can be used as an IF conditional judgment. Today, I will continue to record how to use WHERE.

Some related instruction records are as follows:

  • IN
  • BETWEEN
  • LIKE
  • %, _: wildcard

Basic use of WHERE instruction

WHERE instruction can use the following symbols to judge:

OperatorMeaning
=equal to
!=not equal to
<less than
>more tha
<=less than or equal to
>=more than or equal to

We can also use “logical operators” to string together different judgments.

OperatorMeaning
ANDand
ORor
NOTnot

We still use the world database in MySQL to demo:

select
    Name, SurfaceArea
    from country
    where SurfaceArea > 10000 and SurfaceArea < 20000
    order by SurfaceArea desc;



Output:

NameSurfaceArea
New Caledonia18575.00
Fiji Islands18274.00
Kuwait17818.00
Swaziland17364.00
East Timor14874.00
Bahamas13878.00
Vanuatu12189.00
Falkland Islands12173.00
Gambia11295.00
Qatar11000.00
Jamaica10990.00
Lebanon10400.00

According to the above instructions, we can query the countries where the surface area is between 10000 and 20000.


IN

IN is an instruction that can judge multiple conditions in WHERE instruction, so when we want to judge whether a specific value in the column or table, we don’t have to write a lot of ORs.

For example:

select
    Name, SurfaceArea
    from country
    where SurfaceArea in (10400, 11000, 8000);



Output:

NameSurfaceArea
Lebanon10400.00
Qatar11000.00

We can determine that countries with attribute SurfaceArea as 10400.00 and 11000.00 exist, but with 8000 do not exist.


BETWEEN

BETWEEN can determine whether the value of the field we are looking for lies in a certain interval, and only needs to connect the two values of BETWEEN with AND.

select
    Name, SurfaceArea
    from country
    where SurfaceArea
    between 10000 and 20000
    order by SurfaceArea desc;



Output:

NameSurfaceArea
New Caledonia18575.00
Fiji Islands18274.00
Kuwait17818.00
Swaziland17364.00
East Timor14874.00
Bahamas13878.00
Vanuatu12189.00
Falkland Islands12173.00
Gambia11295.00
Qatar11000.00
Jamaica10990.00
Lebanon10400.00

We can use the BETWEEN instruction to achieve the same result as the above WHERE judgment formula.


LIKE

LIKE instruction is commonly used to match characters, usually with characters such as % and _.

  • %: Unlimited character length matching
  • _: Only one character length match

For example:

select
    Name
    from country
    where Name
    like '%gol%';


Output:

Name
Angola
Mongolia

Two such country names will be found. Because “%xxx%” refers to whether there is a “xxx” in the matching name, there can be “characters of any length” before and after it.

If we modify the command:

select
    Name
    from country
    where Name
    like '%gol_';



Output:

Name
Angola

This time, since our command only matches the number of characters in front of “gol”, it doesn’t matter, but the latter can only accept “one character”, so the result we found this time only returns Angola.

Leave a Reply