MySql进阶教程

mysql查询语法
select * from 表名 * 代表整个表
select 列名 from 表名 where 查询条件表达式
SELECT grde AS 课程 FROM student

查询空行
select sname,semail from student where semial is null

限制固定行
select sname,semail from student limit n //从1-n行都会显示
select sname,semail from student limit n,m //从(n,m]行都会显示

查询排序
升序 SELECT * FROM student ORDER BY result ASC – 升序
降序 SELECT * FROM student ORDER BY result DESC LIMIT 5 --降序

拼接字符
select group_concat(字段名) from 表名

四舍五入
select round(sum(字段名)) from 表名

四舍五入保留n个小数位
select round(sum(字段名),n) from 表名

时间条件查询
WHERE DATE_FORMAT(fctime,’%Y%m’)=DATE_FORMAT(“2018-1-1” , ‘%Y%m’)

最大值,最小值
SELECT MAX(result)FROM student && SELECT price FROM bill ORDER BY price DESC LIMIT 1
SELECT MIN(result)FROM student

其他语法
SELECT * FROM student ORDER BY result DESC LIMIT 5
SELECT * FROM student LIMIT 5
SELECT * FROM student WHERE studentfirs IS NOT NULL AND studentname=‘b’
SELECT * FROM student WHERE studentfirs IS NOT NULL AND studentname=‘b’
UPDATE student SET studentname=‘a’ WHERE studentname=‘b’ OR studentname=‘c’;
UPDATE 表名 SET studentname=‘a’ WHERE studentname=‘b’ OR studentname=‘c’;
SELECT * FROM student WHERE studentfirs<14
1.使用group by 进行分组查询
2.多表查询

分组查询语句
select …from 表名 where … group by… group by 分组的依据

SELECT COUNT() AS 人数, SGrade AS 年级 FROM Students GROUP BY SGrade
SELECT CourseID, AVG(Score) AS 课程平均成绩 FROM Score GROUP BY CourseID ORDER BY AVG(Score)
– 统计每学期男女同学的人数
SELECT COUNT(
) AS 人数,gradeid AS 年级,sex AS 性别 FROM student GROUP BY gradeid,sex ORDER BY gradeid

分组筛选语句
SELECT …… FROM <表名> WHERE …… GROUP BY ……HAVING……
SELECT COUNT() AS 人数,SGrade AS 年级FROM Students GROUP BY SGrade HAVING COUNT()>15
having 用法: 排序后的条件约束

顺序
where ----> group by -------> having ---------->order by

多表联接查询
内联接 (inner join) inner join

SELECT S.SName,C.CourseID,C.Score FROM Score AS C INNER JOIN Students AS S
ON C.StudentID = S.StudentID
SELECT Students.SName, Score.CourseID, Score.Score FROM Students,Score
WHERE Students.StudentID = Score.StudentID
外连接 左外联接 (LEFT JOIN) 右外联接 (RIGHT JOIN)

SELECT S.SName,C.CourseID,C.Score
FROM Score AS C
INNER JOIN Students AS S
ON C.StudentID = S.StudentID

一样等价
SELECT Students.SName, Score.CourseID, Score.Score
FROM Students,Score
WHERE Students.StudentID = Score.StudentID
三表内联接

SELECT
S.SName AS 姓名, CS.CourseName AS 课程, C.Score AS 成绩
FROM Students AS S
INNER JOIN Score AS C ON (S.SCode = C.StudentID)
INNER JOIN Course AS CS ON (CS.CourseID = C.CourseID)
SELECT *,b.uid “用户id” FROM address b , users a where b.uid = a.id //as可省

左外联接 右外联接 right join

SELECT S.SName,C.CourseID,C.Score
FROM Students AS S
LEFT JOIN Score AS C 顺序固定
ON C.StudentID = S.SCode
子查询

SELECT Student_No, Student_Name, Sex, Born_Date, Address
FROM Student
WHERE Born_Date >
(SELECT Born_Date FROM Student WHERE Student_Name=‘李斯文) 唯一性

约束
非空约束(not null
如果在列上定义了not null,那么当插入数据时,必须为列提供数据。
主键约束(primary key):
用于唯一的标识表行的数据,当定义主键约束后,该列不但不能重复而且不能为null。
外键约束(foreign key):
用于定义主表和从表之间的关系,外键约束要定义在从表上,主要则必须具有主键约束,当定义外键约束后,要求外键列数据必须在主表的主键列存在或是为null。
检查约束(check):
用于强制行数据必须满足的条件,假定在age列上定义了check约束,并要求age列值在0~100之间,如果不在0~100之间就会提示出错。

数据库创建视图
双击选中的数据库,在Views鼠标右键“Create View…”,打开编辑窗口,并在窗口中输入代码,代码如下:

CREATE VIEW view_worker_info AS SELECT * FROM t_worker_info;
查看视图信息
show table status like ‘view_worker_info’;

你可能感兴趣的:(MySql)