函数
version
函数
version
运算符的使用
1
正则表达式查询
匹配字符开始的部分
查询用户名以T开始说的用户
REGEXP regexp
查询所有获得奖学金的学员信息.
SELECT id,username FROM student WHERE score》=ANY(SELECT level FROMscholarship)
子查询
联合查询
UNION
UNION ALL
添加删除外键操作
删除部门
DELETE FROM department WHERE id=1;
外键操作
INnnoDB存储引擎才支持
创建部门表department(主表)
id depName depDesc
CREATE TABLE IF NOT EXISTS department(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
depName VARCHAR(20) NOT NULL UNIQUE
)ENGINE=INNODB;
INSERT department(depName)VALUES('教学部') ,
('市场部') ,
('运营部') ,
('督导部') ;
创建员工表employee(字表)
CREATE TABLE IF NOT EXISTS employee(
id SMALLINT AUTO_INCREMENT KEY,
userName VARCHAR(20) NOT NULL UNIQUE,
deoId TINYINT UNSIGNED
外键
FROEING KEY(depId) REFERENCES department(id)
)ENGINE=INNODB;
INSERT employee(username,depId)VALUES('king1’,1),('king2’,2),('king3’,3),('king4’,4);
外链接查询
LEFT JOIN左外
左表为准
右表没有的以NULL替代
RIGHT JOIN右外
右表为准
链接查询
查询user id,username
provinces,proName
SELECT user.id,username,proName FROM user,provinces;
查询user表中id,username,email,sex
查询provinces表中proname
SELECT u,id,u,username,u,email,u.sex,p.proName
FROM user AS u
INNER JOIN provinces AS p 等同CROSS JOIN provinces AS p
ON u.priID=p.id;
更新删除应用orderby和limit
UPDATE cms SET age=age-3 WHERE username = LIKE‘____’
更新前三条记录 limit只能写一个参数
UPDATE user SET age=age+10 LIMIT 3;
按照id降序排列,更新前三条
UPDATE user SET age=age+10 ORDER BY id DESC LIMIT 3;
删除用户性别为男的用户,按照年龄降序排列,删除钱1条记录
DELETE FROM user WEHRE sex=‘ 男’ ORDER BY age DESC LIMIT 1;
limit限制显示条数
分页核心
SELECT & FROM cms LIMIT 3;
分页 第一页3条记录
SELECT & FROM cms LIMIT 0,3;
SELECT id,sex,age,GROUP_CONCAT(username),
COUNT(*) AS totalusers,
MAX(age) AS max_age,
MIN(age) AS min_age,
AVG(age) AS avg_age,
SUM(age) AS sum_age,
FROM cms_user
WHERE id>=1
GROUP BY sex
HAVING COUNT(*)>=2
ORDER BY age DESC
LIMIT 0,2;
ORDER BY 查询结果排序
升ASC 降DESC
分组查询配合聚合函数
GROUP_CONCAT()
查询id,sex,用户名详情按照性别分组
SELECT id,sex,GROUP_CONCAT(username) FROM user GROUP BY sex;
分组查询
GROUP BY
按照用户省份分组
SELECT * FROM user GROUP BY priid;
模糊查询
%代表0个一个或者多个任意字符
_代表1个任意字符
SELECT * FROM user WHERE username LIKE '张%'
待条件查询
删除数据表
DROP TABLE IF EXITS user12;
DRIP TABLE IF EXISTS user1,user2,user3;
登录同时打开指定数据库
mysql -uroot -p -D maizi
添加删除唯一
CREATE TABLE IF NOT EXISTS user12(
id TINYINT UNSIGNED KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
card CHAR(18) NOT NULL,
test VARCHAR(20) NOT NULL,
test1 CHAR(32) NOT NULL,
)
DESC user12 查看
ALTER TABLE name ADD UNIQUE(username);