接昨天续集
直接上干货

-- 今天进行一些常用命令# 昨天知识补充一下# mysql 数据库不区分大小写# show TABLES -- 查看所有表show databasesuse dmshow TABLES -- 查看所有表select * from class_f# 条件查询-- where 筛选符合条件的记录行数-- where 后可以跟一些运算符-- 比较运算符号 > >= < <= != =-- 算数运算符号 + - * /-- 逻辑运算符号 and or notselect * from class_f where age=21select * from class_f where age+1>20-- 如果and和or同时出现,and优先级更高-- 如果有and连接不止一个条件,先按照第一个筛选,-- 第二个条件按照第一个筛选后的再次筛选-- 所以说以后语句有and,尽量将条件苛刻的写在前面 提高执行效率SELECT * from class_f WHERE sex='男' and age = 21SELECT * from class_f WHERE sex='男' or age = 21-- [not] between and 在...之间 包含前后两端的值SELECT * from class_f WHERE age Between 20 and 22-- [not] in 如下条件满足一个就可以select * from class_f Where age in(20,22)select * from class_f Where age not in(20,22)-- like 模糊查询-- %用来代替一个或多个字符-- _用来代替一个字符(有且只有一个)select * from class_f where unames like '东%'select * from class_f where unames like '_木%'select * from class_f where unames like '%鱼%'-- 升序排列-- asc 降序排列-- desc 默认升序select * from class_f ORDER BY age ascselect * from class_f ORDER BY age desc-- select * from class_f ORDER BY age,__,__ desc-- 起别名的3种方式select unames as 姓名,sex as 性别,age as 年龄 from class_f;select unames 姓名,sex 性别,age 年龄 from class_f;select unames "姓名",sex "性别",age "年龄" from class_f;SELECT * FROM class_f-- 函数-- 定义=数据库定义好的(Java API中的方法)-- API(Application Programming Interface,应用程序编程接口---- 函数需要调用才能执行 没有对象 函数直接放置在语句中相当于调用-- 函数可以放置在:-- 查询用来显示的部分--->select 函数(列)from 表格-- 条件筛选的后面 --->select 列 from 表格 where sal > 函数(值)-- 具体讲解下不同函数的用法# 控制流程函数(转换函数)SELECT * from class_fif(条件,值1,值2)SELECT unames if(age>20,'大','小') from class_f;# 字符串函数----String类# length(str) 查看列内容中的长度select unames,length(unames) from class_f;-- concat() 字符串拼接select unames,sex,concat(unames,sex) from class_f;-- substr() 截取字符串SELECT unames,substr(unames,1,4) from class_f-- instr(str,'a') 返回要截取的字符串在源字符串中的位置select unames,INSTR(unames,'东') from class_fselect * from class_f where instr(unames,'东') != 0;-- replace() 替换select unames,replace(unames,'吧','屁') from class_f;-- upper() 改为大写select * from class_fselect unames,UPPER(unames) from class_f;-- lower() 改为大写select unames,lower(unames) from class_f---- 还有很多函数用法-- 具体参考网站https://www.runoob.com/mysql/mysql-functions.htmlSELECT * from class_f-- 分组函数-- count-- count函数是用来统计表中或数组中记录的一个函数-- count(*) 它返回检索行的数目, 不论其是否包含 NULL值。-- COUNT(DISTINCT 字段),返回不同的非NULL值数目;-- 若找不到匹配的项,则COUNT(DISTINCT)返回 0 。-- DISTINCT 去重SELECT * from class_finsert into class_f(unames,sex) values('大鱼','女')select unames,age,count(age) from class_fselect count(*) from class_fselect count(DISTINCT(sex='男')) from class_f-- max() min() avg() sum()SELECT unames,sex,MAX(age) from class_fSELECT unames,sex,min(age) from class_fSELECT sex,count(age),avg(age) from class_f WHERE sex='女'SELECT sex,sum(age) from class_f-- 分组函数 GROUP BYselect count(unames) from student group by classid;-- 添加一列-- 手动添加,不建议用代码了 找到表,右键,-- 设计表,新增一列cid,-- 打开表,并添加数据SELECT * from class_fselect cid,count(unames) from class_f GROUP BY cid-- where优先级 > group by > having > order by-- having 筛选作用-- 再手动添加一列成绩并添加数据-- 比如查询平均成绩高于90分的班级,按逻辑说先分组,平均成绩筛选where-- 但where优先级高;用到关键字havingselect cid,avg(score) from class_f group by cid where avg(score)>90; -- 会报错滴-- 功能一样,优先级不一样select cid,avg(score) from class_f group by cid having avg(score)>90;-- 先筛选 后分组 where group-- 先分组 后筛选 group having-- *在查询的时候一旦分组 行数会减少 想要展示的信息,个数需要与分组条件的个数一致

快12点了,今天先这样吧
文章转载自运维小白超凡奇才,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




