mysql-3

这篇博客介绍了SQL语言在创建和管理数据库表中的应用,包括创建员工表、查看表结构、查询不同职位及其薪资,以及使用聚合函数进行数据统计。内容涉及了数据筛选、排序、分组和聚合操作,如`DISTINCT`、`GROUP BY`、`HAVING`子句,以及`ORDER BY`和`LIMIT`用于结果排序和限制。

练习创建一个表:

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%"; 使用正则     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值