5. MySQL 8.0 reference manual 3.3.4.2 select specific lines

Keywords: MySQL

3.3.4.2 select a specific line

As shown in the previous section, it is easy to retrieve the entire table. As long as the WHERE clause in the statement is omitted SELECT . But usually you don't want to see the whole table, especially when it gets bigger. Instead, you are usually more interested in answering specific questions, in which case you need to specify some restrictions on the information you need. Let's look at the questions about your pet's answers.

You can only select specific rows from the table. For example, if you want to verify your changes to the birth date of a Bowser, select the record for the Bowser as follows:

mysql> SELECT * FROM pet WHERE name = 'Bowser';
+--------+-------+---------+------+------------+------------+
| name   | owner | species | sex  | birth      | death      |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+

It turned out that the record for the year was correct in 1989, not 1979.

String comparisons are usually case insensitive, so you can specify names as' Bowser ',' Bowser ', and so on. The query results are the same.

You can specify criteria on any column, not just name. For example, if you want to know which animals were born in 1998 or later, test the birth column:

mysql> SELECT * FROM pet WHERE birth >= '1998-1-1';
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |
| Puffball | Diane | hamster | f    | 1999-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+

For example, you can combine conditions to locate a female dog:

mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

Previous query uses AND Logical operators. And one more OR Operator:

mysql> SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL  |
| Slim     | Benny | snake   | m    | 1996-04-29 | NULL  |
+----------+-------+---------+------+------------+-------+

AND And OR It can be mixed, though AND High priority OR . If you use two operators at the same time, it is best to use parentheses to clarify how conditions should be grouped:

mysql> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')
    -> OR (species = 'dog' AND sex = 'f');
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

This article is composed of What to learn Web translation

Posted by pranav_kavi on Tue, 31 Mar 2020 04:06:42 -0700