MySQL —— 简单查询与按条件查询
在MySQL中从数据表中查询数据的基本语句时select语句。
  select语句基本语法格式:
      select 查询内容 
      from 表名
      where 表达式
      group by 字段名
      having 表达式
      order by 字段名
      limit 记录数
每一个select语句由多个子句组成。
1. from 表名 指定是从那张表中查询

2. select 查询内容
查询所有字段 select * from 表名;
*通配符:表示所有字段

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)

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

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

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

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

3. where 表达式 (按条件查询)
在MySQL的表查询时,往往并不是需要将所有内容全部查出,而是根据实际需求,查询所需数据
select 查询内容 from 表名 where 表达式;
在MySQL语句中,条件表达式是指select语句的查询条件,在where子句中可以使用关系运算符连
接操作数作为查询条件对数据进行选择。
关系运算符:
=   等于
<>  不等于
!=  不等于
<   小于

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

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

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)

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

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)

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

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

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

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)

在使用distinct指定多个字段时,只有被指定的这些字段的值都相同,才会被认为是重复的
在查询具有一类相同特征的数据时,需要用到模糊查询,这是就需要使用like关键字
select 查询内容 from 表名 where 内容 (not) like ‘匹配的字符串’
百分号通配符 %:表示匹配任意长度的任意字符串

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> select name from name where name like ‘11__’;
±-----+
| name |
±-----+
| 1112 |
| 1122 |
±-----+
2 rows in set (0.00 sec)

如果需要查询带有 % 或 _ 的数据,由于 % 和 _ 是通配符,则需要使用 \ 进行转义
% 表示 %,_ 表示 _
 
有时在查询时为了查询结果更加精确,需要多个限条件,这时就需要 and(&&) 来连接条件

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)

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

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)

注意:在查询时,and 的优先级高于 or
 
聚合函数:
  count()函数:统计记录条数 select count(记录) from 表名

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)

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

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

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

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

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

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

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

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

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

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)

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

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 |
±-----±-----+

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

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)

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

10-07 14:01