MySQL高级语言

x33g5p2x  于2021-12-13 转载在 Mysql  
字(2.6k)|赞(0)|评价(0)|浏览(448)

一.导入数据库

二.select

三.distinct

四.where

五.and or

法一

(root@localhost) [hellodb]> select * from students where age>30 and age<40;

法二

(root@localhost) [hellodb]> select * from students where gender='m' or(age>30 and age<40);

六. in

七.between

八.like 通配符

通配符含义
%表示零个,一个或者多个字符
_下划线表示单个字符
A_Z所有以A开头 Z 结尾的字符串 ‘ABZ’ ‘ACZ’ 'ACCCCZ’不在范围内 下划线只表示一个字符 AZ 包含a空格z
ABC%所有以ABC开头的字符串 ABCD ABCABC
%CBA所有以CBA结尾的字符串 WCBA CBACBA
%AN%所有包含AN的字符串 los angeles
_AN%所有 第二个字母为 A 第三个字母 为N 的字符串
(root@localhost) [hellodb]> select * from students where name like 's%';

九.order by

按关键字排序

(root@localhost) [hellodb]> select * from students where stuid<10 order by age;

十.函数

1.数学函数

函数含义
abs(x)返回x的绝对值
rand()返回0到1的随机数
mod(x,y)返回x除以y以后的余数
power(x,y)返回x的y次方
round(x)返回离x最近的整数
round(x,y)保留x的y位小数四舍五入后的值
sqrt(x)返回x的平方根
truncate(x,y)返回数字 x 截断为 y 位小数的值
ceil(x)返回大于或等于 x 的最小整数
floor(x)返回小于或等于 x 的最大整数
greatest(x1,x2…)返回集合中最大的值
least(x1,x2…)返回集合中最小的值

2. 聚合函数

函数含义
avg()返回指定列的平均值
count()返回指定列中非 NULL 值的个数
min()返回指定列的最小值
max()返回指定列的最大值
sum(x)返回指定列的所有值之和

  1. 字符串函数
函数描述
trim()返回去除指定格式的值
concat(x,y)将提供的参数 x 和 y 拼接成一个字符串
substr(x,y)获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同
substr(x,y,z)获取从字符串 x 中的第 y 个位置开始长度为z 的字符串
length(x)返回字符串 x 的长度
replace(x,y,z)将字符串 z 替代字符串 x 中的字符串 y
upper(x)将字符串 x 的所有字母变成大写字母
lower(x)将字符串 x 的所有字母变成小写字母
left(x,y)返回字符串 x 的前 y 个字符
right(x,y)返回字符串 x 的后 y 个字符
repeat(x,y)将字符串 x 重复 y 次
space(x)返回 x 个空格
strcmp(x,y)比较 x 和 y,返回的值可以为-1,0,1
reverse(x)将字符串 x 反转

十一.group by

(root@localhost) [hellodb]> select classid,sum(age) from students group by classid;

十二.having

十三.别名

  • 在 mysql 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者多次使用相同的表,可以给字段列或表设置别名
  • 方便操作,增强可读性

(root@localhost) [hellodb]> select s.name as name_students, s.stuid as id from students as s;

十四. 子查询

(root@localhost) [hellodb]> select name,age from students where age > (select avg(age) from students);

十五.exists

(root@localhost) [hellodb]> select * from teachers where exists (select teacherid from students where teacherid<1);

十六.连接

1.内连接

(root@localhost) [hellodb]> select * from teachers inner join students on students.teacherid=teachers.tid;

2.左连接

(root@localhost) [hellodb]> select * from students left join teachers on teachers.tid=students.teacherid;

(root@localhost) [hellodb]> select * from teachers left join students on students.teacherid=teachers.tid;

(root@localhost) [hellodb]> select * from students s left join teachers t on s.teacherid=t.tid order by tid;

3.右连接

(root@localhost) [hellodb]> select * from teachers right join students on students.teacherid=teachers.tid;

十七.视图

  • 可以被当作是数据库中的虚拟表,存储查询结果的表。退出数据库后视图还是存在的。
  • 这张虚拟表是真实数据的一个动态映射
  • 这张虚拟表能动态的保存结果集
  • 因为视图和真实表之间是动态同步的关系,所以我们修改虚拟表的同时,真实数据也会收到影响
  • 但是视图除了名字什么都没有,是一个投影,所以不占空间
  • 我们只需要对其做权限的设置,就可保证其安全性
  • 我们可以为视图定义展示的条件,为不同的人群展示不同的网内容
(root@localhost) [hellodb]> create view oyyy_view as select s.name as name1,t.name as name2 from students s,teachers t;

(root@localhost) [hellodb]> drop view oyyy_view;

十八.联集

十九.case

相关文章