mysql之单表查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql之单表查询相关的知识,希望对你有一定的参考价值。

单表查询的语法

select * from 表名  # *所有
select distinct 字段 from  表名  #distinct 去重
           where   #条件
           group by  字段   #分组
           having   #过滤
           order by  字段  #排序
           limit  #限制条数

关键字的优先级

from
where
group by
having
select
distinct
order by
limit

简单查询

创建表
create table employee(
    id int not null unique auto_increment,
    name varchar(20) not null,
    sex enum(male,female) not null default male, #大部分是男的
    age int(3) unsigned not null default 28,
    hire_date date not null,
    post varchar(50),
    post_comment varchar(100),
    salary double(15,2),
    office int, #一个部门一个屋子
    depart_id int
);
#三个部门:教学,销售,运营
insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
(egon,male,18,20170301,老男孩驻沙河办事处外交大使,7300.33,401,1), #以下是教学部,全都是老师
(alex,male,78,20150302,teacher,1000000.31,401,1),
(wupeiqi,male,81,20130305,teacher,8300,401,1),
(yuanhao,male,73,20140701,teacher,3500,401,1),
(liwenzhou,male,28,20121101,teacher,2100,401,1),
(jingliyang,female,18,20110211,teacher,9000,401,1),
(jinxin,male,18,19000301,teacher,30000,401,1),
(成龙,male,48,20101111,teacher,10000,401,1),

(歪歪,female,48,20150311,sale,3000.13,402,2),#以下是销售部门
(丫丫,female,38,20101101,sale,2000.35,402,2),
(丁丁,female,18,20110312,sale,1000.37,402,2),
(星星,female,18,20160513,sale,3000.29,402,2),
(格格,female,28,20170127,sale,4000.33,402,2),

(张野,male,28,20160311,operation,10000.13,403,3), #以下是运营部门
(程咬金,male,18,19970312,operation,20000,403,3),
(程咬银,female,18,20130311,operation,19000,403,3),
(程咬铜,male,18,20150411,operation,18000,403,3),
(程咬铁,female,18,20140512,operation,17000,403,3)
;

查询操作

select * from employee;
select distinct post from employee;
select name,salary*12 from employee;
select name,salary*12 as annual_salary from employee;
select concat(姓名:,name,年薪:,salary*12) as annual_salary from emplyee;

where约束

        1.比较符:>< >= <= <> !=

         2.between 80 and 100 值在80到100之间

         3.in(80,90,100) 值是80或90或100

         4.like ‘egon%‘

               pattern可以是%或_,

               %表示任意多个字符

               _表示一个字符

          5.逻辑运算符:在多个条件直接

#单表查询
select name from employee where post=sale;
#多表查询
select name,salary from employee where post=teacher and salar>10000;
#关键字查询
select name,salary from employee where salary between 10000 and 20000;
#关键字 is null 判断null只能用is
select name,post_comment from employee where post_comment is null;
#关键字in
select name,salary drom employee where salary in (3000,3500,4000,9000);
#关键字like模糊查询
select * from employee where name like eg%;
select * from employee where name like al__‘;

分组查询group by

查看mysql默认下的sql_mode:
select @@global.sql_mode;  
设置sql_mode
set global sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION;

select * from employee group by post;
select post,group_concat(name) from employee group by post;
select post,count(id) as count from employee group by post;

聚合函数

select count(*) from employee;
select count(*) from employee where depart_id = 1;
select max(salary) from employee;
select min(salary) from employee;
select avg(salary) from employee;
select sum(salary) from employee;

having过滤

select post,avg(salary) as new_sa from employee where age>=30 group by post having avg(salary) >10000;

distinct

select count(distinct post) from employee;

查询排序:order by

select * from employee order by salary;
select * from employee order by salary asc;#升序
select * from employee order by salary desc; #降序
select * from employee order by age;
select * from employee order by age,salary desc;

限制查询条件:limit

select * from employee order by salary desc limit 3;
select * from employee order by salary desc limit 0,5; #从0开始显示5条
select * from employee order by salary desc limit 5,5; #从5开始显示5条

正则表达式查询

select * from employee where name regexp ^alex;
select * from employee where name regexp on$;
select * from employee where name regexp m{2};

 

以上是关于mysql之单表查询的主要内容,如果未能解决你的问题,请参考以下文章

MySQL之单表查询

MySQL基础语法之单表查询

MySQL之单表查询

mysql之单表查询

MySQL之单表查询

mysql之单表查询