mysql常用语句

查看版本号

select version();

查看数据库

show databases ;

起别名

select 100*98 as '结果' from dual;

查看表中的字段的详细信息

describe employees;
或者
desc employees;

去重

select distinct department_id from employees;

+号

select last_name+first_name as '姓名' from employees;


mysql中的+号,只有一个功能,就是运算符
其中一方是字符型是,试图将字符型数值转换成数值型,如果转换成功,则继续进行加法运算,若转换失败,则将字符型转为0,如果一方为null,则结果肯定为null。

字符串的拼接

select concat(last_name,first_name) as '姓名' from employees;

其中,concat()在mysql中可以传入多个参数

查看表的详情

desc employees;

ifnull()函数,判断是否为null

select ifnull(commission_pct,0) from employees;

通配符

% 任意多个字符,包含0个字符
_ 任意单个字符
escape 转义的字符

select * from employees where last_name like '_$_%' escape '$';

安全等于 <=>

--安全等于<=>
1.可作为普通运算符的=
2.也可以用于判断是否是NULL 如:

where salary is NULL/(is not NULL) ->where salary <=>NULL

字符串函数

# substr
# 截取从指定索引处指定字符长度的字符
select substr('李莫愁爱上了陆展元',1,3) out_put;

# 截取从指定索引处后面所有字符
select substr('李莫愁爱上了陆展元',7) out_put;

# instr
# 返回子串第一次出现的索引,如果找不到则返回0
select instr('杨不悔爱上了因留下','因留下') as out_oput;

# trim
select length(trim('     张翠山    ')) as out_put;

select trim('a' from 'aaaa张aaaaaa翠山aaaa') as out_put;

# lpad
# 用指定的字符实现⬅️填充指定长度
select lpad('殷素素',10,'*') as out_put;
select lpad('殷素素',2,'*') as out_put;

# rpad
# 用指定的字符实现右️填充指定长度
select rpad('殷素素',10,'*') as out_put;

# replace 替换
select replace('张无忌爱上了周芷若周芷若周芷若周芷若周芷若','周芷若','赵敏') as out_put;

# lower 是将字段中的字符串数据全部转换成小写
select lower('AAA') from emp;
# upper() 是将字段中的字符串数据全部转换大写
select upper('aaa') from dual;
# concat('','','') 将字符串拼接再一起
select concat('www','.baidu','.com') from dual;

数学函数

# 数学函数
# round 四舍五入(先取绝对值,然后再取整)
select round(1.45);
select round(1.55);
select round(1.557777,2);
select round(-1.45);
select round(-1.55);

# ceil 向上取整(返回大于等于该参数的最小整数)
select ceil(1.00);

# floor 向下取整(返回小于等于该参数的最大整数)
select floor(9.99);

# truncate
select truncate(1.69999,1);

# mod取余   a-a/b*b
select mod(10,3);
select mod(-10,-3);

# 日期函数
# now() 现在(返回当前系统日期+时间)
select now();

# curdate() 返回当前系统时间,不包含时间
select curdate();

# curtime() 返回当前时间,不包含日期
select curtime();

# 可以获取指定的部分,年、月、日、小时、分钟、秒
select year(now()) 年;
select year('1998-01-01') 年;

select month(now()) 月;
select monthname(now()) 月;

聚合函数

# sum 求和
select sum(salary) from employees;
# avg 平均数
select avg(salary) from employees;

# max 最大值
select max(salary) from employees;

# min 最小值
select min(salary) from employees;

# count计算个数
select count(salary) from employees;

select sum(distinct salary),sum(salary) from employees;

select count(distinct salary),count(salary)from employees;

# 统计不为null的行数
select count(commission_pct) from employees;
# 统计全部数据
select count(*) from employees;
select count(1) from employees;
# MYISAM引擎下,count(*)效率好
# INNODB引擎下,count(*) 和count(1)效率差不多,count(字段)最低,因为它要去判断是否为null

group by关键字

# 分组查询 group by可以将表中的数据分成若干组
select avg(salary),department_id from employees group by department_id;

select max(salary) ,job_id from employees group by job_id;

select count(*),location_id from departments group by department_id;

select avg(salary),department_id from employees where email like '%a%' group by department_id;


select count(*),department_id from employees group by department_id having count(*)>2;

select max(salary),job_id from employees where commission_pct is not null group by job_id having max(salary)>12000;


select avg(salary),department_id,job_id from employees group by department_id, job_id;
posted @ 2022-02-24 02:03  King-DA  阅读(33)  评论(0)    收藏  举报