mysql 数据操作 单表查询 简单查询 避免重复DISTINCT

Posted minger_lcm

tags:

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

 

 

创建数据库company

create database company charset=utf8;

 

use company;

company.employee
    员工id      id                  int             
    姓名        emp_name            varchar
    性别        sex                 enum
    年龄        age                 int
    入职日期     hire_date           date
    岗位        post                varchar
    职位描述     post_comment        varchar
    薪水        salary              double
    办公室       office              int
    部门编号     depart_id           int


#创建表

 

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
);

 

#查看表结构
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    |                |
| post         | 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    |                |
+--------------+-----------------------+------+-----+---------+----------------+
10 rows in set (0.24 sec)

 

#插入记录
#三个部门:教学,销售,运营

insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
(alex,male,78,20150302,teacher,1000000.31,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)
;

 



#ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
 

#简单查询

SELECT id,name,sex,age,hire_date,post,post_comment,salary,office,depart_id 
    FROM employee;

    SELECT * FROM employee;

    SELECT name,salary FROM employee;

 

查看有哪些职位


mysql> select post from employee;
+-----------+
| post      |
+-----------+
| teacher   |
| teacher   |
| teacher   |
| teacher   |
| teacher   |
| teacher   |
| sale      |
| sale      |
| sale      |
| sale      |
| sale      |
| operation |
| operation |
| operation |
| operation |
| operation |
+-----------+
16 rows in set (0.00 sec)

 


#避免重复DISTINCT
mysql> select distinct post from employee;
+-----------+
| post      |
+-----------+
| teacher   |
| sale      |
| operation |
+-----------+
3 rows in set (0.00 sec)

 















以上是关于mysql 数据操作 单表查询 简单查询 避免重复DISTINCT的主要内容,如果未能解决你的问题,请参考以下文章

MySQL简单查询和单表查询

mysql--单表查询---语句与语法

MySQL单表查询

零基础学MySQL-- 数据库最常用的操作查询基础篇 -- 单表查询

零基础学MySQL-- 数据库最常用的操作查询基础篇 -- 单表查询

mysql 百万级的表怎么处理