MySQL高级查询及常用函数

create table tab_b(
  b_id int,
  b_name varchar(20),
  b_date date
)

create table tab_y(
  y_id int,
  y_xing varchar(20),
  y_ming varchar(20),
  y_date date,
  y_phone varchar(20),
  y_age int,
  y_sex varchar(20),
  b_id int
)

alter table tab_b add primary key(b_id);
alter table tab_b modify b_id int auto_increment;

insert into tab_b(b_name,b_date) values('股东部','1998-1-14');

select *from tab_b


alter table tab_y add primary key(y_id);
alter table tab_y modify y_id int auto_increment;

insert into tab_y(y_xing,y_ming,y_date,y_phone,y_age,y_sex,b_id) values('师','敏','1999-5-5','15896587452',20,'女',4);

select *from tab_y

---------------sql语句----------------
-- 添加一行
insert into tab_y(y_xing,y_ming,y_date,y_phone,y_age,y_sex,b_id) values('师','敏','1999-5-5','15896587452',20,'女',4);

-- 多行添加
insert into tab_y(y_xing,y_ming,y_date,y_phone,y_age,y_sex,b_id) values
('小','敏','1999-8-15','15896857452',20,'女',6),('刘','航','1999-8-5','15878587452',20,'女',5),
('刘','航','1999-8-5','15878587452',20,'女',5);

-- 删除整张表
delete from tab_y

-- 删除一条
delete from tab_y where y_id=12;

-- 删除多条
delete from tab_y where y_id in (13,14);

-- TRUNCATE语句删除
truncate table tab_y

-- 修改一条
update tab_y set y_ming='m敏' where y_id=10

-- 修改多行
update tab_y set y_ming='min敏' where y_id in(9,10);

-- 查询部门为1的员工:姓名 , 年龄 , 工作地点 
select y_xing,y_ming,y_age,y_sex from tab_y where b_id=1

select y_xing as '姓',y_ming as '名',y_age as '年龄',y_sex as '性别' from tab_y

-- limit限定查询返回行
select concat(y_xing,y_ming) from tab_y limit 1,5

-- limit查询后三条数据
select *from tab_y order by y_id desc limit 3

-- 去除重复行
select distinct y_id from tab_y;

-- 查询年龄在20岁以上的学生信息并进行排序(asc,desc)
select *from tab_y where y_age>20 order by y_age desc

-- 查询出年龄为20岁的女生信息
select *from tab_y where y_age=20 and y_sex='女'

-- 查询不再2部门的员工或年龄不到22岁的学生信息
select *from tab_y where y_age>22 or b_id<>2

-- 查询出不是1部门的员工信息
select *from tab_y where b_id != 1

-- 按照员工的年龄以升序进行排列查询。(升序:asc,降序:desc)
select *from tab_y order by y_age asc

-- 查询所有员工的信息按照年龄以降序进行排列和出生日期的升序进行排列。
select *from tab_y order by y_age desc,y_date asc

-- 二表联合查询
select * from tab_b b,tab_y y where b.b_id=y.b_id

------------------函数-----------------

#1.字符串函数
concat()拼接字符串
select concat(y_xing,y_ming) as '姓名' from tab_y
select concat('a','b') from dual

-- length()返回字节数
select length('你好啊') from dual
select length(y_xing) from tab_y

-- char_length()返回字符个数
select char_length('asdf你') from dual

-- upper()转为大写
select upper('fghHHHjj') from dual

-- lower()转为小写
select lower('FGHfghFGH') from dual

-- ltrim()去除字符串左边的空格
select ltrim('   asd   d') from dual

-- rtrim()去除字符串右边的空格
select rtrim(' as d   ') from dual

-- replace()替换
select replace('aaabbbccc','a','f') from dual

-- locate()返回第一个字符串出现的位置(不存在返回0)
select locate('z','cvbxzsd') from dual

-- substring()截取字符串 
select substring('asdfghjk',3,4) from dual


#日期函数
-- now()返回系统当前时间
select now() from dual

-- timestampdiff()返回日期之间的相差{second 秒, minute 分钟,hour 小时,day 天,week 周,month 月,quarter 季,year 年}
select timestampdiff(year,'1997-03-14','1998-03-14') from dual

- date_Add()加日期{second, minute 分钟,hour 小时,day 天,week 周,month 月,quarter 季,year 年}
select date_Add('1997-03-14',interval 1 day) from dual

--dayofweek()返回日期是一周的多少天
select dayofweek('2019-09-5') from dual

-- yearweek()获取年份和第几周
select yearweek('2019-12-30') from dual

-- datediff()返回二个日期相减剩余天数
select datediff('1998-03-14','1997-03-14') from dual

-- date()****年**月**日
select date('1997-03-14 00:00:12') from dual

#查询出本周内点击率最多的前3条新闻信息
SELECT concat(substring(title,1,10) , '...')  AS '新闻标题'
,Clicks as 点击率 FROM NewsInfo
WHERE yearweek(date_format(AddTime,'%Y-%m-%d')) = yearweek(now())
ORDER BY Clicks desc limit 3;

#数学函数
abs()求绝对值
select abs(-12) from dual

-- ceiling()向上取整
select ceiling(20.1) from dual

-- floor()向下取整
select floor(12.9) from dual

-- power(a,n)计算a的n次方
select power(2,3) from dual

-- round(a,n)舍去n位小数点后数字
select round(12455.1235,2) from dual

-- sign(a)若a>0返回1,=0返回0,<0返回-1
select sign(-4) from dual

-- sqrt()开平方
select sqrt(16) from dual

-- pi()返回圆周率
select pi() from dual

-- rand()随机函数
select rand() from dual

#系统函数及其他
convert()类型转换
select convert(452,char(3)) from dual

-- version()返回数据库版本号
select version() from dual

-- last_insert_id()返回最后生成的auto_increment的值
select last_insert_id() from dual

-- ifnull()
select y_age, IFNULL(y_age,y_id) y_phone from tab_y;

-- password(str)/md5(str)对数据加密

-- 返回当前用户登录的计算机名字
select user,host from mysql.user


----------------------mysql高级查询-------------------------


#模糊查询

#用“%”通配多个字符
-- 查询数据库表中姓"白"的学生
select *from tab_y where y_xing like '白%'

-- 查询包含字符"9"的学生信息,需要中间匹配
select *from tab_y where y_phone like '%9%'

-- 查询以字符"武"结尾的学生信息,需要结尾匹配
select *from tab_y where y_ming like '%武'

-- 用"_"通配单个字符

select *from tab_b

--查询"技__"的部门
select *from tab_b where b_name like '技__'

-- 查询所有以、"__部"显示的部门
select *from tab_b where b_name like '__部'

-- 查询所有的第二个字符为"传"的部门信息
select *from tab_b where b_name like '_传%'

-- 转义字符escape

insert into tab_b(b_name,b_date) values('财_武部','1998-03-17')

-- 查询"财_"的学生
select *from tab_b where b_name like '财/_%' escape '/'

select *from tab_y

-- 正则模式REGEXP操作符
-- 查询出所有的电话号以18开头第三位必须为1到7 的员工信息
select *from tab_y where y_phone regexp '^[1][8][1-7]'

-- Between运算符(根据某个字段值的区间范围来查询符合条件的记录,这个范围内有多少数据并不明确)

-- 查询出年龄在20~24之间的学生信息  
select *from tab_y where y_age between 20 and 24
select *from tab_y where y_age between 20 and 24


-- IN运算符(用来匹配某个字段的值是几个可数的选项值之一)

-- 查询出学生家在郑州或洛阳的学生信息
select *from tab_b where b_name in ('销售部','技术部')

-- 聚合函数(sum:求和,avg:求平均,max:最大值,min:最小值,count:求总条数)
select AVG(y_age) from tab_y

#分组查询
create table tab_grade(
   g_id int,
   g_name varchar(20)
)
alter table tab_grade add primary key(g_id);
alter table tab_grade modify g_id int auto_increment;

insert into tab_grade(g_name) values('四年级')

select *from tab_grade

create table tab_class(
   c_id int,
   c_name varchar(20)
)

alter table tab_class add primary key(c_id)
alter table tab_class modify c_id int auto_increment

insert into tab_class(c_name) values('一班'),('二班'),('三班'),('四班')

select *from tab_class

create table tab_student(
   s_id int,
   s_name varchar(20),
   s_sex varchar(20),
   s_age int,
   s_phone varchar(20),
   s_idNumber varchar(18),
   s_performance double(5,2),
   s_date date,
   g_id int,
   c_id int
)


alter table tab_student add primary key(s_id)
alter table tab_student modify s_id int auto_increment

insert into tab_student(s_name,s_sex,s_age,s_phone,s_idNumber,s_performance,s_date,g_id,c_id) 
values('貂蝉','女',21,'18597978965','411588144569874565',80.22,'1997-02-4',2,2),
('孙尚香','女',20,'18796488965','411527144569878542',89.49,'1997-07-12',2,2),
('大桥','女',20,'18796488965','411527144569878542',79.99,'1997-07-12',3,1),
('小乔','女',20,'18796488965','411527144569878542',89.49,'1997-07-12',4,1),
('虞夫人','女',20,'18796488965','411527144569878542',94.19,'1997-07-12',4,1)


-- 查询每个年级的平均分
SELECT  g_id,AVG(s_performance) FROM tab_student GROUP BY g_id;

-- 查询出每个班级的学生人数
select c_id as '年级',count(c_id) from tab_student GROUP BY c_id
select g_id,count(g_id) from tab_student GROUP BY g_id

-- 每个班中学生最小年龄,并结果按照升序排列
select c_id,min(s_age) from tab_student GROUP BY c_id ORDER BY s_age

update from tab_student set s_name='bai' where s_id=18

select *from tab_student
#多列分组查询
-- 统计每个班级的男女生人数
select c_id,count(s_sex),s_sex from tab_student GROUP BY c_id,s_sex

#Having子句-分组查询的条件筛选
-- 查询年级超过超过2个学生,每个年级年龄最小的学生
select g_id,min(s_age),s_name,count(g_id) from tab_student GROUP BY g_id having count(g_id)>2 ORDER BY MIN(s_age)

-- 查询每个年级考试及格总人数和及格平均分,并显示及格平均分在95分以上的年级记录
select g_id,avg(s_performance),count(*) from tab_student 
where s_performance>=60 GROUP BY g_id HAVING avg(s_performance)>95


--------------------多表查询----------------------------
select *from tab_student

#连接查询
-- 1.隐示内连接

-- 查询出每个学生的姓名,年龄,性别和所在班级名称

-- 语法:SELECT * FROM A,B WHERE 条件
select s.s_id,s.s_name,s.s_age,s.s_sex,c.c_name from tab_student s,tab_class c where s.c_id=c.c_id

-- 2.显示内连接
-- 语法:SELECT * FROM A INNER JOIN B ON 条件
select s.s_id,s.s_name,s.s_age,s.s_sex,c.c_name from tab_student s INNER JOIN tab_class c on s.c_id=c.c_id


#外连接
-- 1.左外连接

-- 查询出所有学生的姓名和考试成绩,包括没有参加考试的学生

-- 语法:SELECT * FROM A LEFT OUTER JOIN B ON 条件;
select * from tab_student s LEFT JOIN tab_class c on s.c_id=c.c_id

-- 2.右外连接
-- 语法:SELECT * FROM A RIGHT OUTER JOIN B ON 条件;
select * from tab_student s RIGHT JOIN tab_class c on s.c_id=c.c_id


#交叉连接(笛卡尔:得到的查询结果是两张表的笛卡尔积,也就是用A表中的每条数据都去匹配B表中的所有数据)
-- 语法:SELECT * FROM A,B;
select *from tab_student,tab_class;

#自连接(表与其自身进行连接,在自连接时必须要给表取别名)

--语法:
select s.id, s.productname, p.productname from product s join product p on s.parent_id = p.id;

你可能感兴趣的:(MySQL)