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';
相关推荐
zt1985q8 分钟前
本地部署开源网络书签与内容管理工具 Karakeep 并实现外部访问
运维·服务器·网络·数据库·网络协议·开源
IvorySQL1 小时前
PostgreSQL 日报| PostgreSQL 19 默认 WAL 压缩算法(8 月 8 日)
数据库·postgresql·区块链
油丶酸萝卜别吃1 小时前
MySQL B+ 树查询全过程详解
数据库·mysql
leisoo80972 小时前
股票数据本地化存储实战:JSON、数据库与列式存储的方案对比
jvm·数据库·json
Exclusive_Cat2 小时前
MySQL回表机制解析与优化策略
数据库·sql
Cry丶2 小时前
遗留系统数据迁移实战(九):区域编码迁移与补偿接口设计
mysql·数据治理·数据迁移·区域编码·补偿接口·组织区域
circuitsosk2 小时前
向量数据库选型与性能压测:Milvus、Pinecone、Chroma在真实业务下的对比
数据库·python·pinecone·milvus·向量数据库·chroma
影寂ldy2 小时前
SQL 索引(Index)完整笔记
数据库·笔记·sql
切糕师学AI2 小时前
从压缩到查询:PostgreSQL中JSON数据的存储与处理实践
数据库·postgresql·json
渣渣盟2 小时前
当 Redis 写入成为性能瓶颈时,如何利用 异步批量 Sink 将吞吐量从 1w QPS 提升到 10w+?
数据库·redis·php