MySQL基础之数据管理与常用函数

2018-10-05  本文已影响0人  磊_5d71

记录操作

添加记录

修改记录

删除记录

查询记录

SELECT select_expr,... FROM tbl_name
[WHERE 条件]
[GROUP BY{col_name|position} HAVING 二次筛选]
[ORDER BY{col_name|postion|expr}[ASC|DESC]]
[LIMIT 限制结果集显示条数]

WHERE条件筛选记录

-- 用户名长度为三位的用户(三个下划线)
SELECT * FROM user WHERE username like '___';

GROUP BY 分组

-- 按性别分组,查看组中的用户名有哪些
SELECT GROUP_CONCAT(username),sex FROM user1 
GROUP BY sex;
-- 测试查询操作
CREATE TABLE user1(
id INT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL UNIQUE COMMENT '编号',
age TINYINT UNSIGNED NOT NULL DEFAULT 18  COMMENT '年龄',
sex ENUM('男','女','保密') NOT NULL DEFAULT '保密' COMMENT '性别',
addr VARCHAR(20) NOT NULL DEFAULT '北京',
married TINYINT(1) NOT NULL DEFAULT 0 COMMENT '0代表未结婚,1代表已婚',
salary FLOAT(8,2) NOT NULL DEFAULT 0 COMMENT '薪水'
)ENGINE=INNODB CHARSET=UTF8;
INSERT user1 VALUES(1,'king',23,'男','北京',1,50000);
INSERT user1(username,age,sex,addr,married,salary) VALUES('queen',27,'女','上海',0,25000);
INSERT user1 SET username='imooc',age=31,sex='女',addr='北京',salary=40000;
INSERT user1 VALUES(NULL,'张三',38,'男','上海',0,15000),
(NULL,'张三风',38,'男','上海',0,15000),
(NULL,'张子怡',39,'女','北京',1,85000),
(NULL,'汪峰',42,'男','深圳',1,95000),
(NULL,'刘德华',58,'男','广州',0,115000),
(NULL,'吴亦凡',28,'男','北京',0,75000),
(NULL,'奶茶妹',18,'女','北京',1,65000),
(NULL,'刘嘉玲',36,'女','广州',0,15000);

-- GROUP BY
SELECT * FROM user1 GROUP BY sex;

-- 按性别分组,查看组中的用户名有哪些
SELECT GROUP_CONCAT(username),sex FROM user1 
GROUP BY sex;

-- 按照sex分组,得到用户名详情,并且显示分组中的总人数
SELECT sex,GROUP_CONCAT(username),count(*) AS totalUsers  FROM user1  GROUP BY sex;

-- 按照addr分组,得到用户名的详情,总人数,得到组中年龄的总和,年龄最大值、最小值、平均值
SELECT addr,
GROUP_CONCAT(username) AS userDetail,
COUNT(*) AS totalUser,
SUM(age),
MAX(age),
MIN(age),
AVG(age)
FROM user1
GROUP BY addr;

-- WITH ROLLUP后面跟总条数
SELECT GROUP_CONCAT(username),
COUNT(*)
FROM user1
GROUP BY sex
WITH ROLLUP;

-- 按照字段位置进行分组
SELECT addr,
GROUP_CONCAT(username) AS userDetail,
COUNT(*) AS totalUser,
SUM(age),
MAX(age),
MIN(age),
AVG(age)
FROM user1
GROUP BY 1;

-- 按照addr分组,统计总人数
SELECT COUNT(*),addr,
GROUP_CONCAT(username)
FROM user1
GROUP BY addr;

-- 对于分组结果进行二次筛选,条件是组中总人数>=3
SELECT COUNT(*),addr,
GROUP_CONCAT(username)
FROM user1
GROUP BY addr 
HAVING COUNT(*) >=3; 

ORDER BY 实现排序效果

-- 按照多个字段排序
SELECT id,username,age
FROM user1
ORDER BY age ASC, id ASC;

-- 实现随机记录
SELECT id,username,age
FROM user1
ORDER BY RAND();

LIMIT限制结果集的显示条数

-- 显示前3条记录
SELECT id,username,age
FROM user1
LIMIT 0,3;

-- 显示接下来的3条
SELECT id,username,age
FROM user1
LIMIT 3,3;

高级操作

多表联查

CREATE DATABASE IF NOT EXISTS test2 DEFAULT CHARACTER SET 'UTF8';
USE test2;
CREATE TABLE emp(
id INT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL UNIQUE COMMENT '编号',
age TINYINT UNSIGNED NOT NULL DEFAULT 18  COMMENT '年龄',
sex ENUM('男','女','保密') NOT NULL DEFAULT '保密' COMMENT '性别',
addr VARCHAR(20) NOT NULL DEFAULT '北京',
depId TINYINT UNSIGNED NOT NULL COMMENT '部门对应的编号'
)ENGINE=INNODB CHARSET=UTF8;

INSERT emp(username,age,depId) VALUES('king',24,1),
('queen',25,2),
('imooc',26,1),
('lily',27,1),
('rose',28,3),
('john',29,3);

INSERT emp(username,age,depId)  VALUES('测试用户',39,6);

CREATE TABLE dep(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
depName VARCHAR(50) NOT NULL UNIQUE,
depDesc VARCHAR(100) NOT NULL DEFAULT ''
)ENGINE=INNODB CHARSET=UTF8;
INSERT dep(depName,depDesc) VALUES('PHP教学部','研发PHP课件'),
('JAVA教学部','研发JAVA课件'),
('WEB前端教学部','研发WEB前端课件'),
('IOS教学部','研发IOS课件');


-- 查询 emp id username age  部门名称 dep depName

SELECT emp.id,emp.username,emp.age,dep.depName FROM emp,dep;

SELECT e.id,e.username,e.age,d.depName
FROM emp AS e
INNER JOIN dep AS d
ON e.depId=d.id;

-- 查询emp id username age addr dep id depName depDesc
SELECT e.id,e.username,e.age,e.addr,
d.id,d.depName,d.depDesc
FROM dep AS d
JOIN emp AS e
ON d.id=e.depId;

-- 测试左外连接
SELECT e.id,e.username,e.age,d.depName,d.depDesc
FROM emp AS e
LEFT OUTER JOIN dep AS d
ON e.depId=d.id;

-- 测试右外连接
SELECT e.id,e.username,e.age,d.depName,d.depDesc
FROM emp AS e
RIGHT JOIN dep AS d
ON e.depId=d.id;

外键约束的使用

-- 指定级联操作 DELETE CASCADE UPDATE CASCADE 更新父表相关子表也会更新
ALTER TABLE news
ADD FOREIGN KEY(cateId) REFERENCES news_cate(id)
ON DELETE CASCADE ON UPDATE CASCADE;
-- 添加外键名称

-- 新闻分类表 news_cate
CREATE TABLE news_cate(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
cateName VARCHAR(50) NOT NULL UNIQUE,
cateDesc VARCHAR(100) NOT NULL DEFAULT ''
)ENGINE=INNODB;

-- 新闻表 news
CREATE TABLE news(
id INT UNSIGNED AUTO_INCREMENT KEY,
title VARCHAR(100) NOT NULL UNIQUE,
content VARCHAR(1000) NOT NULL,
cateId TINYINT UNSIGNED NOT NULL,
FOREIGN KEY(cateId) REFERENCES news_cate(id)
)ENGINE=INNODB;

-- SHOW CREATE TABEL news,自动创建了cateId的索引
| news  | CREATE TABLE `news` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `content` varchar(1000) NOT NULL,
  `cateId` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `title` (`title`),
  KEY `cateId` (`cateId`),
  CONSTRAINT `news_ibfk_1` FOREIGN KEY (`cateId`) REFERENCES `news_cate` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

-- 测试cateID外键对应的id不存在,是否可以插入
INSERT INTO news(title,content,cateId) VALUES('A6','SSAW33',45);

-- 测试删除父表中的记录,和删除父表
DELETE FROM news_cate WHERE id =1;

动态创建和删除外键

-- 动态删除外键 ALTER TABLE tbl_name DROP FOREIGN KEY fk_name;
-- 动态添加外键
1、 ALTER TABLE tbl_name
[CONSTRAINT 外键名称] ADD FOREIGN KEY(外键字段) REFERENCES 主表(主键字段);
2、动态添加外键之前表中的记录一定合法的记录,没有脏值,否则外键添加不成功

-- 动态删除外键,删除了外键,当初创建外键自动产生的索引还在
ALTER TABLE news
DROP FOREIGN KEY news_ibfk_1;

-- 添加外键
ALTER TABLE news
ADD FOREIGN KEY (cateId) REFERENCES news_cate(id);

子查询

-- 测试子查询

-- 测试由IN引发的子查询
SELECT *  FROM emp where depId IN(SELECT id from dep);

SELECT *  FROM emp where depId NOT IN(SELECT id from dep);

-- 学员stu
CREATE TABLE stu(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL UNIQUE,
score TINYINT UNSIGNED NOT NULL
);
INSERT stu(username,score) VALUES('king',95),
('queen',75),
('zhangsan',69),
('lisi',78),
('wangwu',87),
('zhaoliu',88),
('tianqi',98),
('ceshi',99),
('tiancai',50);

-- 分数级别level
CREATE TABLE level(
id tinyint UNSIGNED AUTO_INCREMENT KEY,
score TINYINT UNSIGNED COMMENT '分数'
);
INSERT level(score) VALUES(90),(80),(70);

-- 由比较运算符引出的子查询
-- 查询出成绩优秀的学员
SELECT * FROM stu WHERE score >= (SELECT score FROM level where id =1);

-- 由EXISTS引发的子查询
SELECT * FROM emp WHERE EXISTS(SELECT depName FROM dep WHERE id = 10);
-- 带有ANY SOME ALL关键字的子查询
SELECT * FROM stu 
WHERE score >= ANY(SELECT score FROM level);

SELECT * FROM stu 
WHERE score >= SOME(SELECT score FROM level);

SELECT * FROM stu 
WHERE score >= ALL(SELECT score FROM level);

联合查询的使用

-- 创建一个user1表,id username
CREATE TABLE user1(
    id INT UNSIGNED AUTO_INCREMENT KEY,
    username VARCHAR(20)
)SELECT id,username FROM emp;

--  将user表中的用户名写入到user1表中
INSERT user1(username) SELECT username FROM user;

-- 去掉字段重复值
SELECT DISTINCT(username) FROM user2;

无限级分类数据表的设计与实现

自身连接查询
-- 测试自身连接
CREATE TABLE cate(
    id SMALLINT UNSIGNED AUTO_INCREMENT KEY,
    cateName VARCHAR(100) NOT NULL UNIQUE,
    pId SMALLINT UNSIGNED NOT NULL DEFAULT 0
);
INSERT cate(cateName,pId) VALUES('服装',0);
INSERT cate(cateName,pId) VALUES('数码',0);
INSERT cate(cateName,pId) VALUES('箱包',0);

INSERT cate(cateName,pId) VALUES('男装',1);
INSERT cate(cateName,pId) VALUES('女装',1);
INSERT cate(cateName,pId) VALUES('内衣',1);

INSERT cate(cateName,pId) VALUES('电视',2);
INSERT cate(cateName,pId) VALUES('冰箱',2);
INSERT cate(cateName,pId) VALUES('洗衣机',2);

INSERT cate(cateName,pId) VALUES('爱马仕',3);
INSERT cate(cateName,pId) VALUES('LV',3);
INSERT cate(cateName,pId) VALUES('GUCCI',3);

INSERT cate(cateName,pId) VALUES('夹克',4);
INSERT cate(cateName,pId) VALUES('衬衫',4);
INSERT cate(cateName,pId) VALUES('裤子',4);

INSERT cate(cateName,pId) VALUES('液晶电视',10);
INSERT cate(cateName,pId) VALUES('等离子电视',10);
INSERT cate(cateName,pId) VALUES('背投电视',10);

-- 查询所有的分类信息,并且得到其父分类
SELECT s.id ,s.cateName AS sCateName,p.cateName AS pCateName
FROM cate AS s
LEFT JOIN cate AS p
ON s.pId = p.id;

-- 查询所有分类及其子分类
SELECT p.id ,p.cateName AS pCateName,s.cateName AS sCateName
FROM cate AS s
LEFT JOIN cate AS p
ON s.pId = p.id;



sCate 表
id | cateName        | pId |
+----+-----------------+-----+
|  1 | 服装            |   0 |
|  2 | 数码            |   0 |
|  3 | 玩具            |   0 |
|  4 | 男装            |   1 |
|  5 | 女装            |   1 |
|  6 | 内衣            |   1 |
| 10 | 电视            |   2 |
| 11 | 冰箱            |   2 |
| 12 | 洗衣机          |   2 |
| 13 | 爱马仕          |   3 |
| 14 | LV              |   3 |
| 15 | GUCCI           |   3 |
| 16 | 夹克            |   4 |
| 17 | 衬衫            |   4 |
| 18 | 裤子            |   4 |
| 19 | 液晶电视        |  10 |
| 20 | 等离子电视      |  10 |
| 21 | 背投电视        |  10

pCate
id | cateName        | pId |
+----+-----------------+-----+
|  1 | 服装            |   0 |
|  2 | 数码            |   0 |
|  3 | 玩具            |   0 |
|  4 | 男装            |   1 |
|  5 | 女装            |   1 |
|  6 | 内衣            |   1 |
| 10 | 电视            |   2 |
| 11 | 冰箱            |   2 |
| 12 | 洗衣机          |   2 |
| 13 | 爱马仕          |   3 |
| 14 | LV              |   3 |
| 15 | GUCCI           |   3 |
| 16 | 夹克            |   4 |
| 17 | 衬衫            |   4 |
| 18 | 裤子            |   4 |
| 19 | 液晶电视        |  10 |
| 20 | 等离子电视      |  10 |
| 21 | 背投电视        |  10

MySQL常用函数

数学函数的使用

字符串函数

-- 测试字符串函数

-- CHAR_LENGTH(); 得到字符串的字符数
SELECT CHAR_LENGTH('SAHHIA');

-- 以下汉字显示3个字符
SELECT CHAR_LENGTH('你好啊');

-- LENGTH(); 得到字符串的长度,一个中文在utf-8占3位
SELECT LENGTH('你好啊');

-- CONCAT(s1,s2,....); 将字符串合并为一个字符串
SELECT CONCAT('A','B','C');

-- 有null的情况结果就为null
SELECT CONCAT('A','B','C',null);

-- CONCAT_WS(x,s1,s2,....); 以指定分隔符拼接字符串
SELECT CONCAT_WS('-','A','B','C');
SELECT CONCAT_WS('-','A','B','C',NULL);
SELECT CONCAT_WS(NULL,'A','B','C');

-- REVERSE() 字符串反转
SELECT REVERSE('ABCDEFG');

--  LEFT()|RIGHT(); 返回字符串的前几个字符或者后几个字符
SELECT LEFT('HELLO',2),RIGHT('HELLO',2);

-- LPAD()|RPAD();用字符串填充到指定长度
SELECT LPAD('ABC',10,'?');
SELECT RPAD('ABC',10,'!');

-- 去掉字符串两端的空格TRIM()|LTRIM()|RTRIM():

SELECT CONCAT('*',TRIM(' abc '),'*'),CONCAT('*',LTRIM(' abc '),'*'),CONCAT('*',RTRIM(' abc '),'*');

-- REPEAT():重复指定的次数
SELECT REPEAT('HELLO',4);

-- REPLACE():字符串
SELECT REPLACE('HELLO KING','KING','QUEEN');

-- SUBSTRING();截取字符串SUBSTRING 从1开始截取
SELECT SUBSTRING('ABCDEF',1,4);

-- STRCMP();比较字符串
SELECT STRCMP('A','B');

日期时间常用函数

其他常用函数

上一篇下一篇

猜你喜欢

热点阅读