在本篇文章中将给大家讲解下msyql查询操作:

查询

简单查询:select * from '表名';

避免重复:select distinct '字段' from '表名';

条件查询:select 字段,字段 from 表名 where id<=5(条件);

四则运算查询:select id,dep_id,id*dep_id from company.employee5 where id<=5;

定义显示格式一:select id*dep_id as "id and dep_id's sum" from company.employee5 where id<=5;

定义显示格式:SELECT CONCAT(name, ' annual salary: ', salary*14) AS Annual_salary FROM employee5; //定义格式+四个运算,CONCAT是关键字

多条件:select '字段,字段‘ from '表名' WHERRE '条件一' AND '条件二';

关键字between and:

select '字段,字段' from '表名' where 字段 BETWEEN

'条件一' AND '条件二';


排序查询:

select '字段' from '表名' ORDER BY '排序字段';//字段后加DESC正序,ASC反序

限制查询的记录数:

select '字段' from '表名' ORDER BY '字段,DESC|ACS' LIMIT '数字'; //数字有两种的是(5,从初始位置到第五个)(2,5,第三个开始,共显示五个)

使用集合的查询:

select COUNT(*) from '表名';

select COUNT(*) FROM '表名' WHERE dep_id=101;

select MAX(salary) FROM '表名';

select MIN(salary) FROM '表名';

select AVG(salary) FROM '表名';

select SUM(salary) FROM '表名';

select SUM(salary) FROM '表名' WHERE dep_id=101;

分组查询:

select '字段' from '表名' group by 字段; //可参考下列面试题

模糊查询:

select '字段' from '表名' LIKE '关键字';

正则表达式查询:

select * from '表名' where '字段' REGEXP '关键字';

删库

删库:drop database '库名';

删表:drop table '表名'