mysql 命令实操

目录

前言:

--创建表

--向表中插入数据

-字符串函数

--日期函数

--数学函数

 --系统函数

--查询表

--模糊查询

--聚合函数

--连接查询

--等价语句

--左连接查询

--右链接查询

--分组筛选查询


前言

        本次实操使用数据库版本为5.7.62,以下是数据库常用sql语句的实操过程,一块学习一下吧,相信你会有收获的。

--创建表

create table deptlocation

( deptno int primary key,

dept nvarchar(50) ,

  location nvarchar(50)

)

--向表中插入数据

insert into deptlocation values(1,'销售','北京')

insert into deptlocation values(2,'研发','武汉')

insert into deptlocation values(3,'生产','广州')

insert into deptlocation values(4,'人事','上海')

create table emp   --创建表2

( clerkid int primary key ,

name nvarchar(50),

jiguan nvarchar(50),

sex char(2),

salary numeric(8,2),

leader int ,

joindate datetime,

deptno int foreign key references deptlocation(deptno)          --设置外键

)

--向表中插入数据

insert into emp values(1,'周晶晶','武汉','女',4200.50,4,'2012-3-2',3)

insert into emp values(2,'郭新月','山西','女',3500.30,5,'2012-5-12',1)

insert into emp values(3,'毛义盎','广东','男',5000.20,4,'2012-6-2',2)

insert into emp (clerkid,name,jiguan,sex,salary,joindate)

values(4,'刘云溪','襄阳','男',58000.00,'2013-3-6')

insert into emp values(5,'王玉龙','武汉','男',3800.50,4,'2014-9-6',1)

insert into emp values(6,'褚云龙','广西','男',3800.50,5,'2015-2-3',1)

insert into emp values(7,'张曦汉','武汉','男',3800.50,1,'2012-6-9',3)

insert into emp values(8,'华允诚','孝感','男',3800.50,1,'2013-8-9',3)

insert into emp values(9,'张允诚','孝感','男',3500.50,1,'2013-8-9',3)

insert into emp values(10,'张青熙','孝感','女',2800.50,3,'2013-8-9',2)

insert into emp values(11,'李青山','孝感','男',5800.50,3,'2013-8-9',2)

insert into emp (clerkid,name,jiguan) values(12,'安载道','孝感')

--字符串函数

select   charindex('abc','jgg abc',1) --返回5

select len('abc')  --返回5

select upper('abc')  --返回ABC

select ltrim(' abc ')  --返回 abc

select rtrim(' abc ')  --返回abc

select right ('abcdef',3) --返回def

select replace('abcdef','f','g')  --返回abcdeg

select stuff('abcdef',2,3,'123')  --返回a123ef  在字符串中删除指定位置起指定长度的字符并在指定位置插入新的字符串

--日期函数

select getdate() 

select dateadd(mm,-10,getdate())

select datediff(dd,'2000-09-14',getdate())

select datename(dd,getdate())    --指定的日期部分,字符串和整数形式

select datename(week,getdate())

select datename(dw,getdate())

select datepart(dw,getdate())

--数学函数

 select rand()   --随机生成0-1的数(float)

 select abs(-20) --返回:20

 select abs(20) --返回:20

 select ceiling(43.5)  --返回44

 select ceiling(-43.5)  --返回-43

 select floor(43.5)  --返回43

 select floor(-43.5)  --返回-44

 select power(5,2)  --返回25

 select round(3.1415,2) --返回3.14

 select round(rand()*100,0)

 select sign(-43) --返回-1

 select sign(43)   --返回1

 select sign(0) --返回0

 select sqrt(9)  --返回3 

 --系统函数

 select convert(varchar(5),12345)  --返回'12345'

 select CURRENT_USER --返回登录的用户名

 select datalength('数据库aa')  --返回8

 select HOST_NAME()  --返回登录的计算机用户名

--查询表

select * from emp

select * from deptlocation

--模糊查询

select * from emp where name like '张%'

select * from emp where joindate  between '2013-01-01' and '2014-12-12'

select name,jiguan,sex from emp where jiguan in ('北京','武汉')

select * from emp where deptno is null

select  name ,salary from emp

where salary>=4000

order by salary desc

--聚合函数

select sum(salary) as 工资总和 from emp

where salary between 3500 and 5000

select avg(salary) as 平均工资 from emp

where salary between 3500 and 5000

select max(salary) as 最大工资 from emp

where salary between 3500 and 5000

select min(salary) as 最大工资 from emp

where salary between 3500 and 5000

select count(*)  from emp

select count(deptno)  from emp

--连接查询

select name,deptlocation.deptno,sex,salary

from deptlocation

join emp

on emp.deptno=deptlocation.deptno

order by deptno

--等价语句

select name,deptlocation.deptno,sex,salary

from deptlocation,emp

where emp.deptno=deptlocation.deptno

order by deptno

--左连接查询

select name,deptlocation.deptno,sex,salary

from emp

left join deptlocation

on emp.deptno=deptlocation.deptno

order by deptno

--右链接查询

select name,deptlocation.deptno,sex,salary

from emp

right join deptlocation

on emp.deptno=deptlocation.deptno

order by deptno

--分组筛选查询

select  count(*) as 人数,deptno as 部门 from emp

where deptno is not  null

group by deptno

having count(*)>1

order by deptno

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_841103495

你的鼓励将是我创作做大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值