MYSQL基本查询(3)

MYSQL基本查询

各期链接

  1. MYSQL使用(1)
  2. MYSQL约束(2)
  3. MYSQL基本查询(3)
  4. MYSQL内置函数(4)
  5. MySQL复合查询(5)

表的增删改查

CRUD : Create(创建), Retrieve(读取),Update(更新),Delete(删除)

一.Create

创建表语法

--[]里的值为可选
INSERT [INTO] 表名 [(列名1[,列名2,...])] VALUES (1[,2,...]);

1.单行,多行数据全列插入或指定插入

示例

create table Ctext1(
id int primary key auto_increment,
sn int unique,
name varchar(10),
age int
);
-- 全列插入,按创建顺序依次全插入数据
insert into Ctext1 values(1,1001,'张三',18);
-- 指定列插入,按前面指定的列顺序插入数据
insert into Ctext1 (sn,name) values (1002,'李四');
-- 查看表内容
select * from Ctext1;
-- 多行全列插入
insert into Ctext1 values(3,1003,'王五',20),(4,1004,'赵六',23);
-- 多行指定列插入
insert into Ctext1 (sn,name) values (1005,'田七'),(1006,'周八');
-- 多行指定多列插入
insert into Ctext1(sn,name,age) values(1007,'吴九',19),(1008,'郑十',26);
-- 查看表内容
select * from Ctext1;
mysql> -- 查看表内容
mysql> select * from Ctext1;
+----+------+--------+------+
| id | sn   | name   | age  |
+----+------+--------+------+
|  1 | 1001 | 张三   |   18 |
|  2 | 1002 | 李四   | NULL |
+----+------+--------+------+
2 rows in set (0.00 sec)
mysql> -- 查看表内容
mysql> select * from Ctext1;
+----+------+--------+------+
| id | sn   | name   | age  |
+----+------+--------+------+
|  1 | 1001 | 张三   |   18 |
|  2 | 1002 | 李四   | NULL |
|  3 | 1003 | 王五   |   20 |
|  4 | 1004 | 赵六   |   23 |
|  5 | 1005 | 田七   | NULL |
|  6 | 1006 | 周八   | NULL |
|  7 | 1007 | 吴九   |   19 |
|  8 | 1008 | 郑十   |   26 |
+----+------+--------+------+
8 rows in set (0.00 sec)

2.插入存在则更新

语法

insert ... ON DUPLICATE KEY UPDATE 列名=列值[,列名=列值...];

示例
主键与唯一键不能重复否则会报错

-- 主键冲突
insert into Ctext1 values(1,1009,'子鼠',30);
-- 唯一键冲突
insert into Ctext1 (sn,name,age)values(1002,'丑牛',26);
mysql> -- 主键冲突
mysql> insert into Ctext1 values(1,1009,'子鼠',30);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> -- 唯一键冲突
mysql> insert into Ctext1 (sn,name,age)values(1002,'丑牛',26);
ERROR 1062 (23000): Duplicate entry '1002' for key 'sn'

使用插入更新解决

-- 主键冲突,主键冲突的行
insert into Ctext1 values(1,1009,'子鼠',30) on duplicate key update sn=1009,name='子鼠',age=30;
-- 唯一键冲突,修改原来该唯一键冲突的行
insert into Ctext1 (sn,name,age)values(1002,'丑牛',26) on duplicate key update sn=1002,name='丑牛',age=26;
-- 冲突但是和原来值一样
insert into Ctext1 values(3,1003,'王五',20) on duplicate key update sn=1003,name='王五',age=20;
-- 不冲突直接插入
insert into Ctext1 values(9,1010,'寅虎',35) on duplicate key update sn=1010,name='寅虎',age=35;
-- 查看表
select * from Ctext1;
mysql> -- 主键冲突,主键冲突的行
mysql> insert into Ctext1 values(1,1009,'子鼠',30) on duplicate key update sn=1009,name='子鼠',age=30;
Query OK, 2 rows affected (0.01 sec)
mysql> -- 唯一键冲突,修改原来该唯一键冲突的行
mysql> insert into Ctext1 (sn,name,age)values(1002,'丑牛',26) on duplicate key update sn=1002,name='丑牛',age=26;
Query OK, 2 rows affected (0.01 sec)
mysql> -- 冲突但是和原来值一样
mysql> insert into Ctext1 values(3,1003,'王五',20) on duplicate key update sn=1003,name='王五',age=20;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into Ctext1 values(9,1010,'寅虎',35) on duplicate key update sn=1010,name='寅虎',age=35;
Query OK, 1 row affected (0.00 sec)

mysql> select * from Ctext1;
+----+------+--------+------+
| id | sn   | name   | age  |
+----+------+--------+------+
|  1 | 1009 | 子鼠   |   30 |
|  2 | 1002 | 丑牛   |   26 |
|  3 | 1003 | 王五   |   20 |
|  4 | 1004 | 赵六   |   23 |
|  5 | 1005 | 田七   | NULL |
|  6 | 1006 | 周八   | NULL |
|  7 | 1007 | 吴九   |   19 |
|  8 | 1008 | 郑十   |   26 |
|  9 | 1010 | 寅虎   |   35 |
+----+------+--------+------+
9 rows in set (0.00 sec)

– 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
– 1 row affected: 表中没有冲突数据,数据被插入
– 2 row affected: 表中有冲突数据,并且数据已经被更新
– ON DUPLICATE KEY 当发生重复key的时候

3.替换

语法

-- 主键或唯一键冲突
replace into Ctext1 (sn,name) values (1003,'王莽');
-- 没有冲突
replace into Ctext1 (sn,name) values (1011,'卯兔');

示例

mysql> -- 主键或唯一键冲突
mysql> replace into Ctext1 (sn,name) values (1003,'王莽');
Query OK, 2 rows affected (0.01 sec)
mysql> -- 没有冲突
mysql> replace into Ctext1 (sn,name) values (1011,'卯兔');
Query OK, 1 row affected (0.01 sec)
mysql> select * from Ctext1;
+----+------+--------+------+
| id | sn   | name   | age  |
+----+------+--------+------+
|  1 | 1009 | 子鼠   |   30 |
|  2 | 1002 | 丑牛   |   26 |
|  4 | 1004 | 赵六   |   23 |
|  5 | 1005 | 田七   | NULL |
|  6 | 1006 | 周八   | NULL |
|  7 | 1007 | 吴九   |   19 |
|  8 | 1008 | 郑十   |   26 |
|  9 | 1010 | 寅虎   |   35 |
| 12 | 1003 | 王莽   | NULL |  << 删除后插入,所以age为null,主键增加至12
| 13 | 1011 | 卯兔   | NULL |
+----+------+--------+------+
10 rows in set (0.00 sec)

– 主键 或者 唯一键 没有冲突,则直接插入
– 主键 或者 唯一键 如果冲突,则删除后再插入
– 1 row affected: 表中没有冲突数据,数据被插入
– 2 row affected: 表中有冲突数据,删除后重新插入

二. Retrieve

先创建一个表

create table Rtext(
id int primary key auto_increment,
name varchar(10) not null,
math int not null,
chinese int not null,
english int not null
);
insert into Rtext (name,math,chinese,english) values 
 ('张三', 67, 98, 56), 
 ('李四', 87, 78, 77), 
 ('王五', 88, 98, 90), 
 ('赵六', 82, 84, 67), 
 ('田七', 55, 85, 45), 
 ('周八', 70, 73, 78), 
 ('吴九', 75, 65, 30);

1. SELECT基础查询

(1) 全列查找

语法

	select ... from 表名;

示例

mysql> select * from Rtext;
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  1 | 张三   |   67 |      98 |      56 |
|  2 | 李四   |   87 |      78 |      77 |
|  3 | 王五   |   88 |      98 |      90 |
|  4 | 赵六   |   82 |      84 |      67 |
|  5 | 田七   |   55 |      85 |      45 |
|  6 | 周八   |   70 |      73 |      78 |
|  7 | 吴九   |   75 |      65 |      30 |
+----+--------+------+---------+---------+
7 rows in set (0.00 sec)
(2) 指定列查询

语法

select name,math from Rtext;

示例

mysql> select name,math from Rtext;
+--------+------+
| name   | math |
+--------+------+
| 张三   |   67 |
| 李四   |   87 |
| 王五   |   88 |
| 赵六   |   82 |
| 田七   |   55 |
| 周八   |   70 |
| 吴九   |   75 |
+--------+------+
7 rows in set (0.00 sec)
(3) 查询为表达式

语法

select id,name,math+chinese+english total from Rtext; 

示例

mysql> select id,name,math+chinese+english total from Rtext;
+----+--------+-------+
| id | name   | total |
+----+--------+-------+
|  1 | 张三   |   221 |
|  2 | 李四   |   242 |
|  3 | 王五   |   276 |
|  4 | 赵六   |   233 |
|  5 | 田七   |   185 |
|  6 | 周八   |   221 |
|  7 | 吴九   |   170 |
+----+--------+-------+
7 rows in set (0.00 sec)
(4) 结果去重
select chinese from Rtext;
select distinct chinese from Rtext;
mysql> select chinese from Rtext;
+---------+
| chinese |
+---------+
|      98 |
|      78 |
|      98 |
|      84 |
|      85 |
|      73 |
|      65 |
+---------+
7 rows in set (0.00 sec)
mysql> select distinct chinese from Rtext;
+---------+
| chinese |
+---------+
|      98 |
|      78 |
|      84 |
|      85 |
|      73 |
|      65 |
+---------+
6 rows in set (0.00 sec)

2. WHERE条件选择

语法

select * from 表名 where 条件;

比较运算符 说明
<,<=,>, >=,小于,小于等于,大于,大于等于
= 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL
<=> 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1)
!=, <> 不等于
BETWEEN a0 AND a1 范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1)
IN (option, …) 如果是 option 中的任意一个,返回 TRUE(1)
IS NULL 是 NULL
IS NOT NULL 不是 NULL
LIKE 模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字符

逻辑运算符 说明
AND 多个条件必须都为 TRUE(1),结果才是 TRUE(1)
OR 任意一个条件为 TRUE(1), 结果为 TRUE(1)
NOT 条件为 TRUE(1),结果为 FALSE(0)

示例:田某同学,否则要求总成绩 > 200 并且 数学成绩 < 语文成绩 并且 英语成绩 > 80

select name,math,chinese,english,math+chinese+english total 
from Rtext 
where name like '田_' 
or (math+chinese+english > 200 and math < chinese and english > 80); 
mysql> select name,math,chinese,english,math+chinese+english total 
    -> from Rtext 
    -> where name like '田_' 
    -> or (math+chinese+english > 200 and math < chinese and english > 80); 
+--------+------+---------+---------+-------+
| name   | math | chinese | english | total |
+--------+------+---------+---------+-------+
| 王五   |   88 |      98 |      90 |   276 |
| 田七   |   55 |      85 |      45 |   185 |
+--------+------+---------+---------+-------+
2 rows in set (0.00 sec)

3. ORDER BY结果排序

ASC为升序(从小到大)
DESC为降序(从大到小)
默认为ASC。

语法

select ... from 表名 [where ...] order by 列名 [ASC|DESC],[列名 [ASC|DESC]...];

_示例1:查询同学各门成绩,依次按 语文降序,数学升序,英语降序的方式显示 _

select name,chinese,math,english,math+chinese+english total 
from Rtext
order by chinese desc,math asc,english desc;
mysql> select name,chinese,math,english,math+chinese+english total 
    -> from Rtext
    -> order by chinese desc,math asc,english desc;
+--------+---------+------+---------+-------+
| name   | chinese | math | english | total |
+--------+---------+------+---------+-------+
| 张三   |      98 |   67 |      56 |   221 |
| 王五   |      98 |   88 |      90 |   276 |
| 田七   |      85 |   55 |      45 |   185 |
| 赵六   |      84 |   82 |      67 |   233 |
| 李四   |      78 |   87 |      77 |   242 |
| 周八   |      73 |   70 |      78 |   221 |
| 吴九   |      65 |   75 |      30 |   170 |
+--------+---------+------+---------+-------+
7 rows in set (0.00 sec)

4. 筛选分页结果

语法

-- 查询结果从0开始查n条结果
select ... from 表名 [where ...] [order by ...] limit n;
-- 查询结果从s开始,查n条结果
select ... from 表名 [where ...] [order by ...] limit s,n;
-- 查询结果从s开始,查n条结果(同上,语法上更明显)
select ... from 表名 [where ...] [order by ...] limit s offset n;

示例

select * from Rtext;
select * from Rtext limit 3;
select * from Rtext limit 3,3;
select * from Rtext limit 3 offset 3;
mysql> select * from Rtext;
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  1 | 张三   |   67 |      98 |      56 |
|  2 | 李四   |   87 |      78 |      77 |
|  3 | 王五   |   88 |      98 |      90 |
|  4 | 赵六   |   82 |      84 |      67 |
|  5 | 田七   |   55 |      85 |      45 |
|  6 | 周八   |   70 |      73 |      78 |
|  7 | 吴九   |   75 |      65 |      30 |
+----+--------+------+---------+---------+
7 rows in set (0.00 sec)

mysql> select * from Rtext limit 3;
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  1 | 张三   |   67 |      98 |      56 |
|  2 | 李四   |   87 |      78 |      77 |
|  3 | 王五   |   88 |      98 |      90 |
+----+--------+------+---------+---------+
3 rows in set (0.00 sec)

mysql> select * from Rtext limit 3,3;
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  4 | 赵六   |   82 |      84 |      67 |
|  5 | 田七   |   55 |      85 |      45 |
|  6 | 周八   |   70 |      73 |      78 |
+----+--------+------+---------+---------+
3 rows in set (0.01 sec)

mysql> select * from Rtext limit 3 offset 3;
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  4 | 赵六   |   82 |      84 |      67 |
|  5 | 田七   |   55 |      85 |      45 |
|  6 | 周八   |   70 |      73 |      78 |
+----+--------+------+---------+---------+
3 rows in set (0.01 sec)

三. Update

语法

对查询的结果进行更改

update 表名 set 列名=该列需要改的值[,列名=该列需要改的值...][where ...] [order by ...] [limit ...[offset ...]];

示例

select * from Rtext where name like '王_';
-- 将王某的英语都改为80
update Rtext set english = 80 where name like '王_';
select * from Rtext where name like '王_';
mysql> select * from Rtext where name like '王_';
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  3 | 王五   |   88 |      98 |      90 |
+----+--------+------+---------+---------+
1 row in set (0.00 sec)
mysql> -- 将王某的英语都改为80
mysql> update Rtext set english = 80 where name like '王_';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from Rtext where name like '王_';
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  3 | 王五   |   88 |      98 |      80 |
+----+--------+------+---------+---------+
1 row in set (0.00 sec)

四. Delete

1. 对筛选行删除

语法

delete from 表名 [where ...][order by ...][limit ...[offset ...]];

示例

select * from Rtext where name='王五';
delete from Rtext where name='王五';
select * from Rtext where name='王五';
mysql> select * from Rtext where name='王五';
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  3 | 王五   |   88 |      98 |      80 |
+----+--------+------+---------+---------+
1 row in set (0.00 sec)

mysql> delete from Rtext where name='王五';
Query OK, 1 row affected (0.01 sec)

mysql> select * from Rtext where name='王五';
Empty set (0.00 sec)

2. 对整表删除

慎用,只是删除表里的内容,表结构不变。改变表结构用after/drop

语法

delete from 表名;

3.截断表

  1. 只能对整表操作,不能像 DELETE 一样针对部分数据操作;
  2. 实际上 MySQL 不对数据操作,所以比 DELETE 更快,但是TRUNCATE在删除数据的时候,并不经过真正的事
    物,所以无法回滚
  3. 会重置 AUTO_INCREMENT 项

语法

truncate [table] 表名;

五.插入查询结果

语法

insert into 表名 (列类型1[,列类型2...]) select ...

示列

CREATE TABLE soure_table(
id INT,
NAME VARCHAR(10)
);
CREATE TABLE tmp_table LIKE soure_table;
INSERT INTO soure_table (id,NAME) VALUES 
(100,'aaa'),
(200,'bbb'),
(300,'ccc'),
(400,'ddd'),
(100,'aaa');
INSERT INTO tmp_table (id,NAME) SELECT DISTINCT * FROM soure_table;
RENAME TABLE soure_table TO old_table,
tmp_table TO soure_table;
SELECT * FROM old_table;
SELECT * FROM soure_table;

六.聚合函数

1.COUNT([DISTINCT] expr); 统计查询到数据的个数
2.SUM([DISTINCT] expr); 对查询的数据进行求和
3.AVG([DISTINCT] expr); 对查询的数据进行求平均数
4.MAX([DISTINCT] expr); 返回查询数据的最大值
5.MIN([DISTINCT] expr); 返回查询数据的最小值

mysql> select * from Ctext1;
+----+------+--------+------+
| id | sn   | name   | age  |
+----+------+--------+------+
|  1 | 1009 | 子鼠   |   30 |
|  2 | 1002 | 丑牛   |   26 |
|  4 | 1004 | 赵六   |   23 |
|  5 | 1005 | 田七   | NULL |
|  6 | 1006 | 周八   | NULL |
|  7 | 1007 | 吴九   |   19 |
|  8 | 1008 | 郑十   |   26 |
|  9 | 1010 | 寅虎   |   35 |
| 12 | 1003 | 王莽   | NULL |
| 13 | 1011 | 卯兔   | NULL |
+----+------+--------+------+
10 rows in set (0.00 sec)

mysql> select count(*) from Ctext1;
+----------+
| count(*) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)

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

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

mysql> select max(age) from Ctext1;
+----------+
| max(age) |
+----------+
|       35 |
+----------+
1 row in set (0.00 sec)

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

七.group by语句使用

可以对表数据进行分组
语法

select column1, column2, … from table group by column;

准备工作,创建一个雇员信息表(来自oracle 9i的经典测试表)
EMP员工表
DEPT部门表
SALGRADE工资等级表

DROP database IF EXISTS `scott`;
CREATE database IF NOT EXISTS `scott` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
 
USE `scott`;
 
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
  `deptno` int(2) unsigned zerofill NOT NULL COMMENT '部门编号',
  `dname` varchar(14) DEFAULT NULL COMMENT '部门名称',
  `loc` varchar(13) DEFAULT NULL COMMENT '部门所在地点'
);
 
 
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
  `empno` int(6) unsigned zerofill NOT NULL COMMENT '雇员编号',
  `ename` varchar(10) DEFAULT NULL COMMENT '雇员姓名',
  `job` varchar(9) DEFAULT NULL COMMENT '雇员职位',
  `mgr` int(4) unsigned zerofill DEFAULT NULL COMMENT '雇员领导编号',
  `hiredate` datetime DEFAULT NULL COMMENT '雇佣时间',
  `sal` decimal(7,2) DEFAULT NULL COMMENT '工资月薪',
  `comm` decimal(7,2) DEFAULT NULL COMMENT '奖金',
  `deptno` int(2) unsigned zerofill DEFAULT NULL COMMENT '部门编号'
);
 
 
DROP TABLE IF EXISTS `salgrade`;
CREATE TABLE `salgrade` (
  `grade` int(11) DEFAULT NULL COMMENT '等级',
  `losal` int(11) DEFAULT NULL COMMENT '此等级最低工资',
  `hisal` int(11) DEFAULT NULL COMMENT '此等级最高工资'
);
 
 
insert into dept (deptno, dname, loc)
values (10, 'ACCOUNTING', 'NEW YORK');
insert into dept (deptno, dname, loc)
values (20, 'RESEARCH', 'DALLAS');
insert into dept (deptno, dname, loc)
values (30, 'SALES', 'CHICAGO');
insert into dept (deptno, dname, loc)
values (40, 'OPERATIONS', 'BOSTON');
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, null, 20);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7844, 'TURNER', 'SALESMAN', 7698,'1981-09-08', 1500, 0, 30);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100, null, 20);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);
 
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);
 
insert into salgrade (grade, losal, hisal) values (1, 700, 1200);
insert into salgrade (grade, losal, hisal) values (2, 1201, 1400);
insert into salgrade (grade, losal, hisal) values (3, 1401, 2000);
insert into salgrade (grade, losal, hisal) values (4, 2001, 3000);
insert into salgrade (grade, losal, hisal) values (5, 3001, 9999);

示例

mysql> select deptno,avg(sal),max(sal) from emp group by deptno ;
+--------+-------------+----------+
| deptno | avg(sal)    | max(sal) |
+--------+-------------+----------+
|     10 | 2916.666667 |  5000.00 |
|     20 | 2175.000000 |  3000.00 |
|     30 | 1566.666667 |  2850.00 |
+--------+-------------+----------+
3 rows in set (0.01 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值