练习创建一个表:
create table employee(id int primary key 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 ,
poat varchar(20));
mysql> desc employee; #查看创建的表结构
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(3) unsigned | NO | | 28 | |
| hire_date | date | NO | | NULL | |
| poat | varchar(50) | YES | | NULL | |
| post_comment | varchar(100) | YES | | NULL | |
| salary | double(15,2) | YES | | NULL | |
| office | int(11) | YES | | NULL | |
| depart_id | int(11) | YES | | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+
查询语句:
select distinct post from employee; 不重复
select distinct post ,salary * 10 from employee;
select concat('姓名:',name,‘月薪’,salary) as 显示列的别称 ,post form employee;
#字符串拼接 将自己想显示的字段 ,显示在搜索结果中
分组:
select count(id) ,post from employee group by post;
select max(salary),post from employee group by post,id; 分组这个过程不显示
select max(salary),post from employee group by post having max(salary) >20000; having后可以使用聚合函数 where 不行
select * from employee order by age ,salary desc; 以age升序排列 有相同的结果 以salary 降序排列
select * from employee order by age desc ; 降序
select * from employee limit 0,5; 加上第一条在往后拿5 条
select * from employee where name like "an%";
select * from employee where name regexp"^an%"; 使用正则
这篇博客介绍了SQL语言在创建和管理数据库表中的应用,包括创建员工表、查看表结构、查询不同职位及其薪资,以及使用聚合函数进行数据统计。内容涉及了数据筛选、排序、分组和聚合操作,如`DISTINCT`、`GROUP BY`、`HAVING`子句,以及`ORDER BY`和`LIMIT`用于结果排序和限制。
7879

被折叠的 条评论
为什么被折叠?



