MySQL数据库对数据的各项条件查询,多种条件查询,并对查询结果处理。
对导入的数据进行查询:https://teddyou.com/?id=61
导入数据以/etc/passwd为例。
建立库为:userdb
建立表为:user
普通查询:
mysql> select * from user;
数值比较查询:
= | != | > | >= | < | <= |
等于 | 不等于 | 大于 | 大于等于 | 小于 | 小于等于 |
mysql> select * from user where uid <= 3; +--------+--------+------+------+--------+----------+---------------+ | name | passwd | uid | gid | grep | home | shell | +--------+--------+------+------+--------+----------+---------------+ | root | x | 0 | 0 | root | /root | /bin/bash | | bin | x | 1 | 1 | bin | /bin | /sbin/nologin | | daemon | x | 2 | 2 | daemon | /sbin | /sbin/nologin | | adm | x | 3 | 4 | adm | /var/adm | /sbin/nologin | +--------+--------+------+------+--------+----------+---------------+ 4 rows in set (0.00 sec) 还可以有其他特殊用法! mysql> select * from user where uid = gid; mysql> select * from user where uid != gid;
字符比较查询:
= | != |
等于 | 不等于 |
+------+------+------+ | name | uid | gid | +------+------+------+ | bin | 1 | 1 | +------+------+------+ 1 row in set (0.01 sec)
匹配字段没有值或者存在值的:
is null | is not null |
匹配空 | 匹配非空 |
mysql> insert into user (name) values (null); Query OK, 1 row affected (0.04 sec) mysql> select * from user where uid is null; +------+--------+------+------+------+------+-------+ | name | passwd | uid | gid | grep | home | shell | +------+--------+------+------+------+------+-------+ | NULL | NULL | NULL | NULL | NULL | NULL | NULL | +------+--------+------+------+------+------+-------+ 1 row in set (0.00 sec)
逻辑匹配:
or | and |
逻辑或(多个条件满足其中一个) | 逻辑与(多个条件全部满足) |
mysql> select name,gid,uid from user where name='root' or gid=5 or uid=3000 ; +------+------+------+ | name | gid | uid | +------+------+------+ | root | 0 | 0 | +------+------+------+ 1 row in set (0.00 sec)
范围匹配:
in (值列表) | not in (值列表) | between 值1 and 值2 |
在...里... | 不在...里 | 在...之间... |
mysql> select name from user where name in ('root','student'); +---------+ | name | +---------+ | root | | student | +---------+ 2 rows in set (0.00 sec) mysql> select name,uid,shell from user where shell not in ('/bin','/sbin/nologin') and uid < 10; +----------+------+----------------+ | name | uid | shell | +----------+------+----------------+ | root | 0 | /bin/bash | | sync | 5 | /bin/sync | | shutdown | 6 | /sbin/shutdown | | halt | 7 | /sbin/halt | +----------+------+----------------+ 4 rows in set (0.00 sec) mysql> select * from user where uid between 10 and 12; +----------+--------+------+------+----------+------------+---------------+ | name | passwd | uid | gid | grep | home | shell | +----------+--------+------+------+----------+------------+---------------+ | operator | x | 11 | 0 | operator | /root | /sbin/nologin | | games | x | 12 | 100 | games | /usr/games | /sbin/nologin | +----------+--------+------+------+----------+------------+---------------+ 2 rows in set (0.00 sec)
模糊查询:
字段 like '_' | 字段 like '___' | 字段 like 'a%' | 字段 like '%a%' | 字段 like '%a' | 字段 like '_&_' |
匹配结果为单字符的值 | 匹配三个字符的值 | 匹配a开头的值 | 匹配含a的值 | 匹配a结尾的值 | 匹配两个字符以上的值 |
符号 ‘%’为任意值 | 符号 ‘%’为单个字符 |
mysql> select name from user where name like 'a%'; +--------+ | name | +--------+ | abrt | | adm | | apache | | avahi | +--------+ 4 rows in set (0.00 sec) mysql> select name from user where name like '__'; +------+ | name | +------+ | lp | +------+ 1 row in set (0.00 sec) mysql> select name from user where name like '______%______'; +---------------------+ | name | +---------------------+ | gnome-initial-setup | | libstoragemgmt | | setroubleshoot | | systemd-network | +---------------------+ 4 rows in set (0.00 sec)
正则表达式匹配:
语法:select 显示字段 from 数据表 where 查询字段 regexp '正则表达式';
. | ^ | $ | [] | * | | |
单个字符 | 以什么开头 | 以什么结尾 | 指定一组范围 | 前一字符的任意次数 | 或者 |
.a.. | ^a | a$ | [a-z] | .* | ^a|j$ |
四字符,且第二个为a | 以a开头的值 | 以a结尾的值 | 所有小写字符 | 任意值 | 以a开头或者以j结尾 |
mysql> select uid from user where uid regexp '^[83]'; +------+ | uid | +------+ | 3 | | 8 | | 81 | | 32 | | 89 | | 38 | +------+ 6 rows in set (0.00 sec)
去掉重复值:
命令格式:select distinct 字段 from 数据库.数据表;
mysql> select distinct shell from user; +----------------+ | shell | +----------------+ | /bin/bash | | /sbin/nologin | | /bin/sync | | /sbin/shutdown | | /sbin/halt | | /bin/false | | NULL | +----------------+ 7 rows in set (0.00 sec)
四则运算:
+ | - | * | / | % |
加法 | 减法 | 乘法 | 除法 | 取余 |
mysql> select name,uid, uid - 2 from user where uid <= 4; +--------+------+---------+ | name | uid | uid - 2 | +--------+------+---------+ | root | 0 | -2 | | bin | 1 | -1 | | daemon | 2 | 0 | | adm | 3 | 1 | | lp | 4 | 2 | +--------+------+---------+ 5 rows in set (0.00 sec)
利用四则运算,将符合条件的字段重新赋值,操作如下:
mysql> update user set uid=uid+2 where uid <= 3; Query OK, 4 rows affected (0.03 sec) Rows matched: 4 Changed: 4 Warnings: 0 //更新了四条数据 mysql> select name,uid from user where uid < 6; +--------+------+ | name | uid | +--------+------+ | root | 2 | | bin | 3 | | daemon | 4 | | adm | 5 | | lp | 4 | | sync | 5 | +--------+------+ 6 rows in set (0.01 sec)
利用运算工具,将字段重新赋值,并显示为新的一列,操作如下:(按照如下操作数据不会保存!)
mysql> select name,uid, (uid+10) sum from user where uid <= 6; +----------+------+------+ | name | uid | sum | +----------+------+------+ | root | 2 | 12 | | bin | 3 | 13 | | daemon | 4 | 14 | | adm | 5 | 15 | | lp | 4 | 14 | | sync | 5 | 15 | | shutdown | 6 | 16 | +----------+------+------+ 7 rows in set (0.00 sec)
函数运算查询:
avg(字段) | sum(字段) | min(字段) | max(字段) | count(字段) |
求平均值 | 求和 | 取最小值 | 取达最大值 | 统计数量 |
mysql> select sum(uid) from user; +----------+ | sum(uid) | +----------+ | 76342 | +----------+ 1 row in set (0.00 sec) mysql> select count(name) from user; +-------------+ | count(name) | +-------------+ | 42 | +-------------+ 1 row in set (0.00 sec)
查询结果排序:
order by 字段 | order by 字段 asc | order by 字段 desc |
默认使用默认值,从小到大 | 从小到大 | 从大到小 |
mysql> select name,uid from user where uid>10 and uid<20 order by uid; +----------+------+ | name | uid | +----------+------+ | operator | 11 | | games | 12 | | ftp | 14 | +----------+------+ 3 rows in set (0.00 sec)
指定查询结果显示的行数:
注意:本命令查询中间行数从0开始,即0为第1行。
limit 3 | limit 0,2 | limit 1,2 |
显示前三行 | 显示前两行,从第一行开始往后取两行 | 显示2-3行,从第二行开始往后取两行 |
mysql> select name,uid,gid from user where gid < 1; //先看一下表单,方便比较 +----------+------+------+ | name | uid | gid | +----------+------+------+ | root | 2 | 0 | | sync | 5 | 0 | | shutdown | 6 | 0 | | halt | 7 | 0 | | operator | 11 | 0 | +----------+------+------+ 5 rows in set (0.00 sec) mysql> select name,uid,gid from user where gid < 1 limit 3; //显示前三行 +----------+------+------+ | name | uid | gid | +----------+------+------+ | root | 2 | 0 | | sync | 5 | 0 | | shutdown | 6 | 0 | +----------+------+------+ 3 rows in set (0.00 sec) mysql> select name,uid,gid from user where gid < 1 limit 1,2; //显示2-3行 +----------+------+------+ | name | uid | gid | +----------+------+------+ | sync | 5 | 0 | | shutdown | 6 | 0 | +----------+------+------+ 2 rows in set (0.00 sec)
发表评论