首页 技术 正文
技术 2022年11月12日
0 收藏 806 点赞 3,583 浏览 5407 个字

MySQL —— 简单查询与按条件查询

在MySQL中从数据表中查询数据的基本语句时select语句。
  select语句基本语法格式:
      select 查询内容 
      from 表名
      where 表达式
      group by 字段名
      having 表达式
      order by 字段名
      limit 记录数
每一个select语句由多个子句组成。

1. from 表名 指定是从那张表中查询

2. select 查询内容

查询所有字段 select * from 表名
*通配符:表示所有字段

MySQL —— 基本查询方法

mysql> select * from test;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | A | 4 |
| 2 | B | 7 |
| 3 | C | 5 |
| 4 | D | 12 |
+------+------+------+
4 rows in set (0.06 sec)

MySQL —— 基本查询方法

查询部分字段 select 字段名 from 表名;

MySQL —— 基本查询方法

mysql> select name from test;
+------+
| name |
+------+
| A |
| B |
| C |
| D |
+------+
4 rows in set (0.00 sec)

MySQL —— 基本查询方法

在MySQL表中,每个字段的数据可以当做变量处理
查询所需的某个字段数据处理后的结果:select 字段处理方式 from 表名

MySQL —— 基本查询方法

mysql> select age-3  from test;
+-------+
| age-3 |
+-------+
| 1 |
| 4 |
| 2 |
| 9 |
+-------+
4 rows in set (0.11 sec)

MySQL —— 基本查询方法

3. where 表达式 (按条件查询)

在MySQL的表查询时,往往并不是需要将所有内容全部查出,而是根据实际需求,查询所需数据
select 查询内容 from 表名 where 表达式;

在MySQL语句中,条件表达式是指select语句的查询条件,在where子句中可以使用关系运算符
接操作数作为查询条件对数据进行选择。
关系运算符:
=   等于
<>  不等于
!=  不等于
<   小于
>   大于
<=  小于等于
>=  大于等于

例如查询年龄大于5的信息

MySQL —— 基本查询方法

mysql> select * from test where age > 5;
+------+------+------+
| id | name | age |
+------+------+------+
| 2 | B | 7 |
| 4 | D | 12 |
+------+------+------+
2 rows in set (0.04 sec)

MySQL —— 基本查询方法

in的关键字查询

查询某个指定集合内的记录 select 查询内容 from 表名 where 条件 in(指定内容);

MySQL —— 基本查询方法

mysql> select * from test where age in (5, 12);
+------+------+------+
| id | name | age |
+------+------+------+
| 3 | C | 5 |
| 4 | D | 12 |
+------+------+------+
2 rows in set (0.00 sec)

MySQL —— 基本查询方法

带有between and 关健字查询

查询某个在给定范围内的记录 select 查询内容 from 表名 where 条件 between 值1 and 值2

MySQL —— 基本查询方法

mysql> select * from test where age between 5 and 12;
+------+------+------+
| id | name | age |
+------+------+------+
| 2 | B | 7 |
| 3 | C | 5 |
| 4 | D | 12 |
+------+------+------+
3 rows in set (0.07 sec)

MySQL —— 基本查询方法

查询某些为空NULL  或 非空的记录 select 查询内容 from 表名 where 条件 is(not) NULL

MySQL —— 基本查询方法

mysql> select * from test where age is NULL;
+------+------+------+
| id | name | age |
+------+------+------+
| 6 | F | NULL |
+------+------+------+
1 row in set (0.00 sec)

MySQL —— 基本查询方法

在查询时过滤掉重复的值:select distinct 字段名 from 表名;字段名表示要过滤重复记录的字段

MySQL —— 基本查询方法

mysql> select num from a;
+------+
| num |
+------+
| 5 |
| 10 |
| 15 |
| 10 |
| 15 |
| 5 |
| 10 |
+------+
7 rows in set (0.00 sec)
mysql> select distinct num from a;
+------+
| num |
+------+
| 5 |
| 10 |
| 15 |
+------+
3 rows in set (0.00 sec)

MySQL —— 基本查询方法

在使用distinct指定多个字段时,只有被指定的这些字段的值都相同,才会被认为是重复的

在查询具有一类相同特征的数据时,需要用到模糊查询,这是就需要使用like关键字
select 查询内容 from 表名 where 内容 (not) like ‘匹配的字符串’
百分号通配符 %:表示匹配任意长度的任意字符串

MySQL —— 基本查询方法

mysql> select name from name;
+------+
| name |
+------+
| 1112 |
| 1122 |
| 1222 |
| 2111 |
+------+
4 rows in set (0.00 sec)
mysql> select name from name where name like '11%';
+------+
| name |
+------+
| 1112 |
| 1122 |
+------+
2 rows in set (0.00 sec)

MySQL —— 基本查询方法

下划线通配符 _ :表示匹配任意单个字符,如果需要匹配多个字符,则需要使用多个 _

MySQL —— 基本查询方法

mysql> select name from name where name like '11__';
+------+
| name |
+------+
| 1112 |
| 1122 |
+------+
2 rows in set (0.00 sec)

MySQL —— 基本查询方法

如果需要查询带有 % 或 _ 的数据,由于 % 和 _ 是通配符,则需要使用 \ 进行转义
\% 表示 %,\_ 表示 _

有时在查询时为了查询结果更加精确,需要多个限条件,这时就需要 and(&&) 来连接条件

MySQL —— 基本查询方法

mysql> select cat_id, cat_name, parent_id from category;
+--------+---------------------------+-----------+
| cat_id | cat_name | parent_id |
+--------+---------------------------+-----------+
| 1 | 手机类型 | 0 |
| 2 | CDMA手机 | 1 |
| 3 | GSM手机 | 1 |
| 4 | 3G手机 | 1 |
| 5 | 双模手机 | 1 |
| 6 | 手机配件 | 0 |
| 7 | 充电器 | 6 |
| 8 | 耳机 | 6 |
| 9 | 电池 | 6 |
| 11 | 读卡器和内存卡 | 6 |
| 12 | 充值卡 | 0 |
| 13 | 小灵通/固话充值卡 | 12 |
| 14 | 移动手机充值卡 | 12 |
| 15 | 联通手机充值卡 | 12 |
+--------+---------------------------+-----------+
14 rows in set (0.00 sec)
mysql> select cat_id, cat_name, parent_id from category
-> where cat_id > 7 and parent_id = 6;
+--------+-----------------------+-----------+
| cat_id | cat_name | parent_id |
+--------+-----------------------+-----------+
| 8 | 耳机 | 6 |
| 9 | 电池 | 6 |
| 11 | 读卡器和内存卡 | 6 |
+--------+-----------------------+-----------+
3 rows in set (0.05 sec)

MySQL —— 基本查询方法

有时在查询时,只需要数据满足某些条件中的某一个,这时就需要使用 or(||) 来连接条件

MySQL —— 基本查询方法

mysql> select cat_id, cat_name, parent_id from category where cat_id = 3 or cat_id = 9;
+--------+-----------+-----------+
| cat_id | cat_name | parent_id |
+--------+-----------+-----------+
| 3 | GSM手机 | 1 |
| 9 | 电池 | 6 |
+--------+-----------+-----------+
2 rows in set (0.02 sec)

MySQL —— 基本查询方法

注意:在查询时,and 的优先级高于 or

聚合函数:

count()函数:统计记录条数 select count(记录) from 表名

MySQL —— 基本查询方法

mysql> select * from test;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | A | 4 |
| 2 | B | 7 |
| 3 | C | 5 |
| 4 | D | 12 |
| 5 | E | 0 |
| 6 | F | NULL |
+------+------+------+
6 rows in set (0.01 sec)
mysql> select count(name) from test;
+-------------+
| count(name) |
+-------------+
| 6 |
+-------------+
1 row in set (0.09 sec)

MySQL —— 基本查询方法

sum()函数:计算表中某个字段值的总和,select sum(字段名) from 表名

MySQL —— 基本查询方法

mysql> select sum(age) from test;
+----------+
| sum(age) |
+----------+
| 28 |
+----------+
1 row in set (0.00 sec)

MySQL —— 基本查询方法

avg()函数:计算表中某个字段的平均值 select avg(字段名) from 表名

MySQL —— 基本查询方法

  mysql> select avg(age) from test;
+----------+
| avg(age) |
+----------+
| 5.6000 |
+----------+
1 row in set (0.00 sec)

MySQL —— 基本查询方法

  max()函数:返回表中某个字段中的最大值

MySQL —— 基本查询方法

  mysql> select max(age) from test;
+----------+
| max(age) |
+----------+
| 12 |
+----------+
1 row in set (0.04 sec)

MySQL —— 基本查询方法

  min()函数:返回表中某个字段中的最小值

MySQL —— 基本查询方法

  mysql> select min(age) from test;
+----------+
| min(age) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)

MySQL —— 基本查询方法

  分组查询:

在对数据表中的数据进行统计时,需要将数据按照一定的特征分组统计,此时就需
    要使用分组查询  select 查询内容 from 表名 group by 分组依据 [having表达式条件]

MySQL —— 基本查询方法

  mysql> select * from test;
+------+------+------+-------+
| id | name | age | class |
+------+------+------+-------+
| 1 | A | 4 | 1 |
| 2 | B | 7 | 1 |
| 3 | C | 5 | 1 |
| 4 | D | 12 | 2 |
| 5 | E | 0 | 2 |
| 6 | F | 8 | 3 |
+------+------+------+-------+
6 rows in set (0.00 sec) mysql> select max(age) from test group by class;
+----------+
| max(age) |
+----------+
| 7 |
| 12 |
| 8 |
+----------+
3 rows in set (0.03 sec)

MySQL —— 基本查询方法

对查询结果进行排序
    select 查询内容 from 表名 order by 排序条件 asc/desc,asc表示升序 desc表示降序

MySQL —— 基本查询方法

  mysql> select name, age from test order by age asc;
+------+------+
| name | age |
+------+------+
| E | 0 |
| A | 4 |
| C | 5 |
| B | 7 |
| F | 8 |
| D | 12 |
+------+------+
6 rows in set (0.00 sec)
mysql> select name, age from test order by age desc;
+------+------+
| name | age |
+------+------+
| D | 12 |
| F | 8 |
| B | 7 |
| C | 5 |
| A | 4 |
| E | 0 |
+------+------+

MySQL —— 基本查询方法

限制查询:
    在查询时,可能需要只显示部分数据,这是需要限制查出来的数据数量
    select 查询内容 from 表名 limit 偏移量m 记录数n,表示从第m+1个记录开始查询出n条记录

MySQL —— 基本查询方法

  mysql> select name, age from test order by age asc limit 2, 2;
+------+------+
| name | age |
+------+------+
| C | 5 |
| B | 7 |
+------+------+
2 rows in set (0.00 sec)

MySQL —— 基本查询方法

where 与 having:
    where 与 having关键字都用于设置条件表达式对查询结果进行过滤,区别是having后面可以跟聚合
    函数,而where不能,通常having关键字都与group by 一起使用,表示对分组后的数据进行过滤

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,129
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,601
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,444
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,218
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,852
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,940