本篇文章给大家带来的内容是介绍mysql怎么进行单表查询?单表查询的语句。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
首先创建数据表
# 创建表 mysql> create table company.employee5( id int primary key auto_increment not null, name varchar(30) not null, sex enum('male','female') default 'male' not null, hire_date date not null, post varchar(50) not null, job_description varchar(100), salary double(15,2) not null, office int, dep_id int ); # 插入数据 mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values ('jack','male','20180202','instructor','teach',5000,501,100), ('tom','male','20180203','instructor','teach',5500,501,100), ('robin','male','20180202','instructor','teach',8000,501,100), ('alice','female','20180202','instructor','teach',7200,501,100), ('tianyun','male','20180202','hr','hrcc',600,502,101), ('harry','male','20180202','hr',null,6000,502,101), ('emma','female','20180206','sale','salecc',20000,503,102), ('christine','female','20180205','sale','salecc',2200,503,102), ('zhuzhu','male','20180205','sale',null,2200,503,102), ('gougou','male','20180205','sale','',2200,503,102); # 查看表结构 mysql> desc employee5;+-----------------+-----------------------+------+-----+---------+----------------+| field | type | null | key | default | extra |+-----------------+-----------------------+------+-----+---------+----------------+| id | int(11) | no | pri | null | auto_increment || name | varchar(30) | no | | null | || sex | enum('male','female') | no | | male | || hire_date | date | no | | null | || post | varchar(50) | no | | null | || job_description | varchar(100) | yes | | null | || salary | double(15,2) | no | | null | || office | int(11) | yes | | null | || dep_id | int(11) | yes | | null | |+-----------------+-----------------------+------+-----+---------+----------------+
查询语法select 字段1,字段2... from 表名 where 条件 group by field having 筛选 order by field limit 限制条数;
查看表中所有数据mysql> select * from employee5;+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+| id | name | sex | hire_date | post | job_description | salary | office | dep_id |+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+| 1 | jack | male | 2018-02-02 | instructor | teach | 5000.00 | 501 | 100 || 2 | tom | male | 2018-02-03 | instructor | teach | 5500.00 | 501 | 100 || 3 | robin | male | 2018-02-02 | instructor | teach | 8000.00 | 501 | 100 || 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 || 5 | tianyun | male | 2018-02-02 | hr | hrcc | 600.00 | 502 | 101 || 6 | harry | male | 2018-02-02 | hr | null | 6000.00 | 502 | 101 || 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 || 8 | christine | female | 2018-02-05 | sale | salecc | 2200.00 | 503 | 102 || 9 | zhuzhu | male | 2018-02-05 | sale | null | 2200.00 | 503 | 102 || 10 | gougou | male | 2018-02-05 | sale | | 2200.00 | 503 | 102 |+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+10 rows in set (0.00 sec)
简单查询简单查询mysql> select * from employee5;mysql> select name, salary, dep_id from employee5;
去重distinctmysql> select post from employee5;mysql> select distinct post from employee5;注:不能部分使用distinct,通常仅用于某一字段。
通过四则运算查询mysql> select name, salary, salary*14 from employee5;mysql> select name, salary, salary*14 as annual_salary from employee5;mysql> select name, salary, salary*14 annual_salary from employee5;
定义显示格式concat() 函数用于连接字符串mysql> select concat(name, ' annual salary: ', salary*14) as annual_salary from employee5;
条件查询a、语法 select * from 表名 where 条件 b、比较运算符 大于 小于 大于等于 小于等于 不等于 > < >= <= !=或<>c、逻辑运算符 并且 或者 非 and or notd、模糊查询 like % 表示任意多个任意字符 _ 表示一个任意字符e、范围查询 in 表示在一个非连续的范围内 between...and... 表示在一个连续的范围内f、空判断 判断空:is null 判断非空:is not null g、优先级 小括号,not 比较运算符, 逻辑运算符 and比or优先级高,如果同时出现并希望先选or,需要结合()来使用
单条件查询mysql> select name,post from employee5 where post='hr';
多条件查询mysql> select name,salary from employee5 where post='hr' and salary>5000;
关键字 between and查询mysql> select name,salary from employee5 where salary between 5000 and 15000;mysql> select name,salary from employee5 where salary not between 5000 and 15000;
关键字 is null 查询mysql> select name,job_description from employee5 where job_description is null;mysql> select name,job_description from employee5 where job_description is not null;mysql> select name,job_description from employee5 where job_description='';
关键字in集合查询mysql> select name, salary from employee5 where salary=4000 or salary=5000 or salary=6000 or salary=9000 ;mysql> select name, salary from employee5 where salary in (4000,5000,6000,9000) ;mysql> select name, salary from employee5 where salary not in (4000,5000,6000,9000) ;
关键字like模糊查询通配符’%’mysql> select * from employee5 where name like 'al%';通配符’_’mysql> select * from employee5 where name like 'al___';
查询排序按单列排序mysql> select * from employee5 order by salary;mysql> select name, salary from employee5 order by salary asc;mysql> select name, salary from employee5 order by salary desc;
按多列排序mysql> select * from employee5 order by hire_date desc,salary asc;# 先按入职时间,再按薪水排序mysql> select * from employee5 order by hire_date desc, salary desc;# 先按职位,再按薪水排序mysql> select * from employee5 order by post, salary desc;
分页查询 limitmysql> select * from employee5 order by salary desc limit 5; //默认初始位置为0mysql> select * from employee5 order by salary desc limit 0,5;mysql> select * from employee5 order by salary desc limit 3,5; //从第4条开始,共显示5条
聚合函数查询 a、count(*) 表示计算总行数,括号中可以写*和列名 b、max(列) 表示求此列的最大值 c、min(列) 表示求此列的最小值 d、sun(列) 表示求此列的和 e、avg(列) 表示求此列的平均值 mysql> select count(*) from employee5;mysql> select count(*) from employee5 where dep_id=101;mysql> select max(salary) from employee5;mysql> select min(salary) from employee5;mysql> select avg(salary) from employee5;mysql> select sum(salary) from employee5;mysql> select sum(salary) from employee5 where dep_id=101;
分组查询单独使用group by关键字分组mysql> select post from employee5 group by post;注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数
group by关键字和group_concat()函数一起使用
# 按照id分组,并查看组内成员mysql> select dep_id,group_concat(name) from employee5 group by dep_id;mysql> select dep_id,group_concat(name) as emp_members from employee5 group by dep_id;
group by和集合函数一起使用
# 按照dep_id 分组, 并计算组内成员工资总和mysql> select dep_id,sum(salary) from employee5 group by dep_id;# 按照dep_id分组,并计算组内成员工资平均值mysql> select dep_id,avg(salary) from employee5 group by dep_id;
正则表达式查询mysql> select * from employee5 where name regexp '^j';mysql> select * from employee5 where salary regexp '[5]+.*';mysql> select * from employee5 where salary regexp '[5]{2}.*';
以上就是mysql怎么进行单表查询?单表查询的语句的详细内容。