mysql查询语句
创始人
2024-06-02 09:12:07
0

执行sql文件

source D:\sql文件.sql

 查询当前使用的数据库

select database();

 起别名

select 字段名 as 别名 from dept;

 条件查询语句

3.列的数学运算select 工资*12 from 表名;= 等于    查看工资为800的员工名称  select name from emp where sal = 800;<>或 != 不等于查看信息不等一800的员工名称select name from emp where sal != 800;<小于<= 小于等于>大于>=大于等于between ... and ....两个值之间        查询薪资在2450在3000之前的员工名称   between and   遵循左小右大select name from emp where 工资 between 2450 and 3000		is null 为 null (is not null不为空)查询员工的津贴为null的select * from emp where 津贴 is null;查询员工的津贴不为null的select * from emp where is not null;and 并且查询工作岗位为开发,并且工资大于2500select * from emp where 岗位=‘开发’ and 工资>2500or 或者查询工作岗位为开发或者工资为2500的select * from emp where 岗位=‘开发’ or 工资>2500and和or同时使用查询工资>2500并且部门编号为10 or 为20的select * from emp where 工资 > 2500 and (编号 = 10 or 编号 = 20)in 包含,相当于多个 or (not in 不在这个范围中)查询工作岗位为    开发 or 人事的员工select * form emp where 岗位 in (“开发”,"人事");not not可以取非,主要用在 is 或in 中is nullis not null in not in查询工作岗位不为    开发 or 人事的员工select * from emp where 岗位 not in ("开发","人事")ike like称为模糊查询,支持%下划线匹配%匹配任意多个字符下划线,一个下划线只匹配一个字符找出名字中还有o的select * from emp where name like '%O%';结果:   jones,scott,ford找出名字以T结尾的select * from emp where name like '%T';找出名字为以k开始的select * from emp where name like 'k%'找出第二个字母为a的select * from where name like '_A%';找出第三个字母是R的select * from where name like '__R%';找出名字中带有 _ 的人       !加 \ 进行转义select * from where name like '%\_%';

排序

#降序select * from where emp order by 工资 desc;#升序
select * from where emp order by  工资  asc;   默认升序#先按照工资升序拍,薪资相同再按照名字升序拍
select * from where emp order by 工资 asc , name asc;#找出工资在1250和3000之间按薪资降序排序
select * from where emp between 1250 and 3000 order by 薪资 desc;

单行处理的常见函数

lower()  转换小写大写字母全部转化为小写字母select lower(name) from 表名;
upper()   转换大写小写字母全部转化为大写字母select lower(name) from 表名;substr()    取字符串长度内容去查询名字的第一个字母    substr(被截取的字符串,起始下标,截取长度),起始下标不能为0select substr(name,1,1) from 表名;concat()进行字符串拼接select concat(name,age) from 表名;结果:小米10length 取长度select length(name) from 表名;

 聚合函数

max()最大值#找出工资最高的select max(工资) from 表名;min()最小值#找出最低工资select min(工资) from 表名;
avg()平均值select avg(工资) from 表名;sum()求和select sum(工资) from 表名count()计数#查询信息的个数select count(工资) from 表名;

分组查询

按照工作岗位分组,让后对工资求和select job,sum(工资) from 表名  group by job;找出 每个部门 ,不同工作岗位的最高薪资select max(工资) from 表名 group by 部门,jobhaving可以对分完组之后的数据进一步过滤,having不能单独使用,having不能代替where,having必须和group by联合使用例子:以部门编号分组,查询出部门最大工资并且大于3000的select 编号,max(工资) from 表名 group by 部门编号 having max(sal) > 3000;

查询结果去重

distinct去重

select distinct job from 表名;

笛卡尔积现象:第一张表的名字都会和第二张表的内容去进行匹配一遍

解决方法:加上限制条件即可

内连接SQL92语法第一种:select ename,dname from emp,dept where emp.deptno = dept.deptno;第二种:   给表起别名select ename,dname from emp e,dept d where e.deptno = dept.deptno; 
SQL99语法select e.ename,d.dname from emp e join dept d on e.deptno = d.deptno;

 内连接之不等值连接

 内连接之子连接

        这是a表

        这是b表

查询出该员工的领导,并输出员工名所对应的领导名

select a.ename as '员工名',b.ename as '领导名' from emp a join emp b on a.mgr = b.empno;

 外连接

右外连接
right代表什么:表示将jons关键字右边的这张表堪称主表,主要是为了将这张表的数据全部查询出来,捎带着关联查询右边的表。
在外连接当中,这两张表链接,产生了主次关系。
select e.ename,d.dname from emp e right join dept d on e.deptno = d.deptno;

 结果为

 

 子查询        子查询可以出现在select,from,where后面,查询的都可以做为一张表

        where后面的子查询

                查询出工资大于最低员工的工资

select 工资 from 表名 where 工资 > (select min(工资)  from  表名);

 union的用法

        就是合并两张表的结果,要比 in 的效率要高

select ename,job from emp where job = '开发'
union
select ename,job from emp where job = '人事';

limit的使用     limit  开始下标  条数       limit从第0条开始

               将查询结果集的一部分取出

按照薪资降序,去除排在前5名的员工?

select ename,sal from emp order by  sal desc limit 5;

 (页码-1)* 3   通用的

插入数据语句

给那个字段插,表名后面的()就写那个字段名,其他的为null
insert into 表名(no,data,name) values(1,20,"小米");

插入日期数据

        mysql的日期格式:%Y年       %m月    %d日    %h时   %i分   %s秒

        长日期格式:%Y-%m-%d %h:%i:%s

        短日期格式:    %Y-%m-%d
        str_to_date:字符串转日期类型date

insert into 表名(id,name,birth) values(1,"张三",str_to_date('01-10-1990','%d-%m-%Y'));id:int
name:varchar
birth:date如果写入的函数正好时字符串类型,就不用str_to_date
段日期插入date
insert into 表名(id,name,birth) values(1,"张三",'1990-01-02');
长日期插入datetime
inster into 表名(id,name,birth) values(1,"张三","199-10-02 14:48:20");inster into 表名(id,name,birth) values(1,"张三",now());   #长日期now()获取当前时间

date_format:将日期类型转换成特点格式的字符串

相关内容

热门资讯

北方盆栽桔子树怎么养 北方盆栽桔子树怎么养盆栽橘子如管理不当,往往只开花、不结果或少结果,甚至不开花。要让盆栽橘子年年开花...
马克龙施压英国承认巴勒斯坦国 据新华社电 英国媒体4日披露,法国总统埃马纽埃尔·马克龙正向英国首相基尔·斯塔默施加压力,试图让后者...
十二星座男最喜欢哪一种类型的女... 十二星座男最喜欢哪一种类型的女孩子,为什么?在我看来,对于水瓶座的男生来说,更喜欢那些娇小可爱的女孩...
马斯克宣布“美国党”成立 当地时间7月5日,美国企业家埃隆·马斯克在社交媒体平台X上发文称,“美国党”于当日成立,以还给人民自...
12生肖中哪肖权威最大? 12生肖中哪肖权威最大?在十二生肖中权威最大的动物肯定是山中的大王老虎,因为它是百兽之王。
当别人和你说,我只要你好好的就... 当别人和你说,我只要你好好的就行,怎么回答?当别人和你说,我只要你好好的就行,怎么你好,这个要看是谁...
汪伦是怎样解释十里桃花的 汪伦是怎样解释十里桃花的汪伦解释说:“十里桃花是指十里外的桃花渡;万家酒店是指桃花潭西有个姓万的人家...
成都有没有比较好的景观(园林)... 成都有没有比较好的景观(园林)设计的手绘培训?你可以去明思源问问看那儿是专门做室内设计跟园林设计培训...
把狂浪生改编成故事,算侵权吗 把狂浪生改编成故事,算侵权吗我想根据狂浪生的歌词编成一个写辛弃疾的故事,不会在网上发表,用来交学校的...
关于 “接纳”,的名人故事? 关于 “接纳”,的名人故事?楚霸王项羽就是因为没有接纳别人的意见在鸿门宴上没有杀死刘备所以才有后来的...