MySQL数据库表查询

测试表company.employee5

mysql> create database company;

#创建一个库;

创建一个测试表:

bash 复制代码
 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
      );

插入数据:

bash 复制代码
   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> use company

简单查询语法:

select 字段名称,字段名称2 from 表名 条件

复制代码
简单查询
mysql> select * from employee5;

多字段查询:
mysql> select id,name,sex from employee5;

有条件查询:where

bash 复制代码
   mysql> select id,name from employee5 where id<=3;
   #查询表中id大于等于三的
   mysql> select id,name,salary from employee5 where salary>2000;

设置别名:as

bash 复制代码
 mysql> select id,name,salary as "salry_num" from employee5 where salary>5000;
 给 salary 的值起个别名,显示值的表头会是设置的别名

统计记录数量:count()

bash 复制代码
 mysql> select count(*) from employee5;
 #统计表中的记录数量
 统计字段得到数量:
 mysql> select count(id) from employee5;
#统计表中id的记录数量
 避免重复DISTINCT:表里面的数据有相同的
 mysql> select distinct post from employee5;
                                     #字段      表名
#查找post字段中重复的字符

复制表

bash 复制代码
1.复制表结构+记录 
 语法:create table 新表 select * from 旧表;
 mysql> create table new_t1 select * from employee5;
 
2.复制单个字段和记录:
 mysql> create table new_t2(select id,name from employee5);

3.多条件查询: and ----和,并且

bash 复制代码
   语法: select   字段,字段2 from   表名   where   条件 and  条件;
   mysql> SELECT name,salary from employee5 where post='hr' AND salary>1000;
   mysql> SELECT name,salary from employee5 where post='instructor' AND salary>1000;

4.多条件查询: or ----或者

bash 复制代码
 语法:       select   字段,字段2 from   表名   where   条件   or   条件;
 mysql> select name from employee5 where salary>5000 and salary<10000 or dep_id=102;
 mysql> select name from employee5 where salary>2000 and salary<6000 or dep_id=100;

5.关键字 BETWEEN AND 什么和什么之间。

bash 复制代码
 mysql> SELECT name,salary FROM employee5 where salary between 5000 and 15000;
 查找salary中5000到15000之间的内容
 mysql> SELECT name,salary FROM employee5 WHERE salary NOT BETWEEN 5000 AND 15000;
 mysql> select name,dep_id,salary from employee5 where  not salary>5000;
 注:not  给条件取反

6.关键字IS NULL 空的

bash 复制代码
 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;  #-取反 不是null
  mysql> SELECT name,job_description FROM employee5 WHERE job_description=''; 
  #什么都没有==空
  NULL说明:
          1、等价于没有任何值、是未知数。
          2、NULL与0、空字符串、空格都不同,NULL没有分配存储空间。
          3、对空值做加、减、乘、除等运算操作,结果仍为空。
          4、比较时使用关键字用"is null"和"is not null"。
          5、排序时比其他数据都小(索引默认是升序排列,小→大),所以NULL值总是排在最前。

7.排序查询 order by :指令,在mysql是排序的意思。

bash 复制代码
 mysql> select name,salary from employee5 order by salary; #-默认从小到大排序。
 mysql> select name,salary from employee5 order by salary desc; #降序,从大到小

8.limit 限制

bash 复制代码
   mysql> select * from employee5 limit 5;  #只显示前5行
   mysql> select name,salary from employee5 order by salary desc limit 0,1; #从第几行开始,打印一行
   查找什么内容从那张表里面降序排序只打印第二行。
   注意:
   0-------默认第一行
   1------第二行  依次类推...
   mysql> SELECT * FROM employee5 ORDER BY salary DESC LIMIT 0,5;  #降序,打印5行
   mysql> SELECT * FROM employee5 ORDER BY salary DESC LIMIT 4,5;  #从第5条开始,共显示5条

9.分组查询 :group by

bash 复制代码
  mysql> select count(name),post from employee5 group by post;
    +-------------+------------+
    | count(name) | post       |
    +-------------+------------+
    |           2 | hr         |
    |           4 | instructor |
    |           4 | sale       |
    +-------------+------------+
    count可以计算字段里面有多少条记录,如果分组会分组做计算

10.函数

复制代码
 max() 最大值
 mysql> select max(salary) from employee5;
 min()最小值
 select min(salary) from employee5;
avg()平均值
  select avg(salary) from employee5;
 now()  现在的时间
select now();
 sum()  计算和
mysql>select sum(salary) from employee5 where post='sale';
相关推荐
廿一夏4 小时前
MySql存储引擎与索引
数据库·sql·mysql
lzhdim6 小时前
SQL 入门 15:SQL 事务:从 ACID 到四种常见的并发问题
数据库·sql
瀚高PG实验室6 小时前
瀚高企业版V9.1.1在pg_restore还原备份文件时提示extract函数语法问题
数据库·瀚高数据库
TDengine (老段)7 小时前
TDengine Tag 设计哲学与 Schema 变更机制
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
敲个大西瓜7 小时前
Java项目常用数据归档方式
mysql
YOU OU8 小时前
Spring IoC&DI
java·数据库·spring
Muscleheng8 小时前
Navicat连接postgresql时出现‘datlastsysoid does not exist‘报错
数据库·postgresql
kyriewen9 小时前
面试官让我查各部门工资最高的员工,我用AI三秒写出窗口函数,他愣了
后端·mysql·面试
小码工作室9 小时前
使用 HAVING 进行 MySQL 集合筛选
mysql
罗超驿9 小时前
18.事务的隔离性和隔离级别:MySQL面试高频考点全解析
数据库·mysql·面试