在本篇文章中將給大家講解下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 '表名'