Home  >  Article  >  Database  >  Examples of implementing conditional restriction statements in mysql (2)

Examples of implementing conditional restriction statements in mysql (2)

黄舟
黄舟Original
2017-09-09 15:01:031259browse

5.limit statement:
Syntax: select field name from indicates limit starting line, query several lines;

mysql> select * from 4a limit 0,3;
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 刘一   |   18 | 叶平   |    1 | 语文   |    1 |    1 |    56 |
| 刘一   |   18 | 贺高   |    2 | 数学   |    1 |    2 |    78 |
| 刘一   |   18 | 杨艳   |    3 | 英语   |    1 |    3 |    67 |
+--------+------+--------+------+--------+------+------+-------+
3 rows in set (0.00 sec)

Note: The first line of the starting line is 0 instead of 1 .

6. like statement:

Syntax: select field name from table name where field name like condition;

mysql> select * from 4a where sage like "%9" or sage like "%8";
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 刘一   |   18 | 叶平   |    1 | 语文   |    1 |    1 |    56 |
| 刘一   |   18 | 贺高   |    2 | 数学   |    1 |    2 |    78 |
| 刘一   |   18 | 杨艳   |    3 | 英语   |    1 |    3 |    67 |
| 刘一   |   18 | 周磊   |    4 | 物理   |    1 |    4 |    58 |
| 钱二   |   19 | 叶平   |    1 | 语文   |    2 |    1 |    79 |
| 钱二   |   19 | 贺高   |    2 | 数学   |    2 |    2 |    81 |
| 钱二   |   19 | 杨艳   |    3 | 英语   |    2 |    3 |    92 |
| 钱二   |   19 | 周磊   |    4 | 物理   |    2 |    4 |    68 |
| 李四   |   18 | 贺高   |    2 | 数学   |    4 |    2 |    88 |
| 李四   |   18 | 杨艳   |    3 | 英语   |    4 |    3 |    90 |
| 李四   |   18 | 周磊   |    4 | 物理   |    4 |    4 |    93 |
| 赵六   |   19 | 叶平   |    1 | 语文   |    6 |    1 |    35 |
| 赵六   |   19 | 贺高   |    2 | 数学   |    6 |    2 |    68 |
| 赵六   |   19 | 周磊   |    4 | 物理   |    6 |    4 |    71 |
+--------+------+--------+------+--------+------+------+-------+
14 rows in set (0.00 sec)
mysql> select * from 4a where sage like "%9" or sage like "%8" or sage like "%%";
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 刘一   |   18 | 叶平   |    1 | 语文   |    1 |    1 |    56 |
| 刘一   |   18 | 贺高   |    2 | 数学   |    1 |    2 |    78 |
| 刘一   |   18 | 杨艳   |    3 | 英语   |    1 |    3 |    67 |
| 刘一   |   18 | 周磊   |    4 | 物理   |    1 |    4 |    58 |
| 钱二   |   19 | 叶平   |    1 | 语文   |    2 |    1 |    79 |
| 钱二   |   19 | 贺高   |    2 | 数学   |    2 |    2 |    81 |
| 钱二   |   19 | 杨艳   |    3 | 英语   |    2 |    3 |    92 |
| 钱二   |   19 | 周磊   |    4 | 物理   |    2 |    4 |    68 |
| 张三   |   17 | 叶平   |    1 | 语文   |    3 |    1 |    91 |
| 张三   |   17 | 贺高   |    2 | 数学   |    3 |    2 |    47 |
| 张三   |   17 | 杨艳   |    3 | 英语   |    3 |    3 |    88 |
| 张三   |   17 | 周磊   |    4 | 物理   |    3 |    4 |    56 |
| 李四   |   18 | 贺高   |    2 | 数学   |    4 |    2 |    88 |
| 李四   |   18 | 杨艳   |    3 | 英语   |    4 |    3 |    90 |
| 李四   |   18 | 周磊   |    4 | 物理   |    4 |    4 |    93 |
| 王五   |   17 | 叶平   |    1 | 语文   |    5 |    1 |    46 |
| 王五   |   17 | 杨艳   |    3 | 英语   |    5 |    3 |    78 |
| 王五   |   17 | 周磊   |    4 | 物理   |    5 |    4 |    53 |
| 赵六   |   19 | 叶平   |    1 | 语文   |    6 |    1 |    35 |
| 赵六   |   19 | 贺高   |    2 | 数学   |    6 |    2 |    68 |
| 赵六   |   19 | 周磊   |    4 | 物理   |    6 |    4 |    71 |
+--------+------+--------+------+--------+------+------+-------+
21 rows in set (0.00 sec)

You can see the double percent sign %% It is a full match and can match all records in the table. So, can even NULL values ​​be matched?

The answer is no:

mysql> select * from 4a;
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 刘一   |   18 | 叶平   |    1 | 语文   |    1 |    1 |    56 |
| 刘一   |   18 | 贺高   |    2 | 数学   |    1 |    2 |    78 |
| 刘一   |   18 | 杨艳   |    3 | 英语   |    1 |    3 |    67 |
| 刘一   |   18 | 周磊   |    4 | 物理   |    1 |    4 |    58 |
| 钱二   |   19 | 叶平   |    1 | 语文   |    2 |    1 |    79 |
| 钱二   |   19 | 贺高   |    2 | 数学   |    2 |    2 |    81 |
| 钱二   |   19 | 杨艳   |    3 | 英语   |    2 |    3 |    92 |
| 钱二   |   19 | 周磊   |    4 | 物理   |    2 |    4 |    68 |
| 张三   |   17 | 叶平   |    1 | 语文   |    3 |    1 |    91 |
| 张三   |   17 | 贺高   |    2 | 数学   |    3 |    2 |    47 |
| 张三   |   17 | 杨艳   |    3 | 英语   |    3 |    3 |    88 |
| 张三   |   17 | 周磊   |    4 | 物理   |    3 |    4 |    56 |
| 李四   |   18 | 贺高   |    2 | 数学   |    4 |    2 |    88 |
| 李四   |   18 | 杨艳   |    3 | 英语   |    4 |    3 |    90 |
| 李四   |   18 | 周磊   |    4 | 物理   |    4 |    4 |    93 |
| 王五   |   17 | 叶平   |    1 | 语文   |    5 |    1 |    46 |
| 王五   |   17 | 杨艳   |    3 | 英语   |    5 |    3 |    78 |
| 王五   |   17 | 周磊   |    4 | 物理   |    5 |    4 |    53 |
| 赵六   |   19 | 叶平   |    1 | 语文   |    6 |    1 |    35 |
| 赵六   |   19 | 贺高   |    2 | 数学   |    6 |    2 |    68 |
| 赵六   |   19 | 周磊   |    4 | 物理   |    6 |    4 |    71 |
| NULL   | NULL | NULL   | NULL | NULL   | NULL | NULL |    93 |
+--------+------+--------+------+--------+------+------+-------+
22 rows in set (0.05 sec)

7.distinct statement:
Syntax: select distinct field name from table name;

mysql> select distinct sname from 4a;           
+--------+
| sname  |
+--------+
| 刘一   |
| 钱二   |
| 张三   |
| 李四   |
| 王五   |
| 赵六   |
| NULL   |
+--------+
mysql> select distinct sname, sage from 4a;
+--------+------+
| sname  | sage |
+--------+------+
| 刘一   |   18 |
| 钱二   |   19 |
| 张三   |   17 |
| 李四   |   18 |
| 王五   |   17 |
| 赵六   |   19 |
| NULL   | NULL |
+--------+------+
7 rows in set (0.00 sec)
mysql> select distinct sname, sage,tname from 4a;
+--------+------+--------+
| sname  | sage | tname  |
+--------+------+--------+
| 刘一   |   18 | 叶平   |
| 刘一   |   18 | 贺高   |
| 刘一   |   18 | 杨艳   |
| 刘一   |   18 | 周磊   |
| 钱二   |   19 | 叶平   |
| 钱二   |   19 | 贺高   |
| 钱二   |   19 | 杨艳   |
| 钱二   |   19 | 周磊   |
| 张三   |   17 | 叶平   |
| 张三   |   17 | 贺高   |
| 张三   |   17 | 杨艳   |
| 张三   |   17 | 周磊   |
| 李四   |   18 | 贺高   |
| 李四   |   18 | 杨艳   |
| 李四   |   18 | 周磊   |
| 王五   |   17 | 叶平   |
| 王五   |   17 | 杨艳   |
| 王五   |   17 | 周磊   |
| 赵六   |   19 | 叶平   |
| 赵六   |   19 | 贺高   |
| 赵六   |   19 | 周磊   |
| NULL   | NULL | NULL   |
+--------+------+--------+
22 rows in set (0.00 sec)

Yes It can be seen that distinct no longer returns unique records when used on multiple columns.
In addition, distinct can also be used with some mathematical operation functions:

mysql> select count(distinct sname) from 4a;
+-----------------------+
| count(distinct sname) |
+-----------------------+
|                     6 |
+-----------------------+
1 row in set (0.08 sec)
mysql> select sum(distinct score) from 4a;
+---------------------+
| sum(distinct score) |
+---------------------+
|                1193 |
+---------------------+
1 row in set (0.06 sec)
mysql> select max(distinct score) from 4a;
+---------------------+
| max(distinct score) |
+---------------------+
|                  93 |
+---------------------+
1 row in set (0.06 sec)
mysql> select min(distinct score) from 4a;
+---------------------+
| min(distinct score) |
+---------------------+
|                  35 |
+---------------------+
1 row in set (0.00 sec)
mysql> select min(distinct score-1) from 4a;
+-----------------------+
| min(distinct score-1) |
+-----------------------+
|                    34 |
+-----------------------+
1 row in set (0.08 sec)

There are too many examples to list one by one.

The above is the detailed content of Examples of implementing conditional restriction statements in mysql (2). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn