单表查询
表查询
语法:
select 字段名称,字段名称2 from 表名 条件
简单查询
mysql> select * from cs; 查看这个表下所有内容
多字段查询:
mysql> select id,name,sex from cs;
有条件查询:where
mysql> select id,name from cs where id<=3; 查找id,name,从cs表 条件<=3的
mysql> select id,name,salary from cs where salary>2000; 查找id,name salary,从cs表 条件salary>=2000的
设置别名:as
mysql> select id,name,salary as “薪资” from cs where salary>5000;
统计记录数量:count()
mysql> select count(*) from cs; 统计表里有多少数据
统计字段得到数量:
mysql> select count(id) from cs; 统计i字段有多少数据
避免重复DISTINCT:表里面的数据有相同的
mysql> select distinct post from cs;
#字段 表名
复制表
表复制:key不会被复制: 主键、外键和索引
复制表
1.复制表结构+记录 (key不会复制: 主键、外键和索引)
语法:create table 新表 select * from 旧表;
案例:
mysql> create table cs1 select * from cs;
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> show tables;
±-------------+
| Tables_in_gg |
±-------------+
| cs |
| cs1 |
| g1 |
2.复制单个字段和记录:
mysql> create table cs3(select id,name from cs);
3.多条件查询: and ----和/与
语法: select 字段,字段2 from 表名 where 条件 and where 条件;
mysql> select name,salary from cs where post=‘hr’ and salary>1000;
mysql> SELECT name,salary from cs where post=‘instructor’ AND salary>1000;
4.多条件查询: or ----或者/或
语法: select 字段,字段2 from 表名 where 条件 or 条件;
mysql> mysql> select name from cs where salary>5000 and salary<10000 or dep_id=102;
mysql> select name from cs where salary>2000 and salary<6000 or dep_id=100;
5.关键字 BETWEEN AND 什么和什么之间。
mysql> SELECT name,salary FROM cs WHERE salary BETWEEN 5000 AND 15000;
mysql> SELECT name,salary FROM cs WHERE salary NOT BETWEEN 5000 AND 15000;
mysql> select name,dep_id,salary from cs where not salary>5000;
注:not 给条件取反
7.关键字IN集合查询
一般查询:
mysql> select name,salary from cs where salary=4000 or salary=5000 or salary=6000 or salary=9000;
IN集合查询
mysql> select name,salary from cs where salary in (4000,5000,6000,9000);
查询多个同一字段不同的值,可以这样查询
mysql> select name,salary from cs where salary not in (4000,5000,6000);
取反
排序查询 order by
:指令,在mysql是排序的意思。
mysql> select name,salary from cs order by salary; #-默认从小到大排序。
mysql> select name,salary from cs order by salary desc;#降序,从大到小
limit 限制
mysql> select * from cslimit 5; #只显示前5行
mysql> select name,salary from cs order by salary desc limit 5; #从第几行开始,打印一行
查找什么内容从那张表里面降序排序只打印第二行。
mysql> select name,salary from cs order by salary desc limit 3,6;#默认从第3条开始显示3条
分组查询 :group by
mysql> select count(name),post from cs group by post;
count可以计算字段里面有多少条记录,如果分组会分组做计算
查找 统计(条件:工资=2000)的有几个人(count(name)),分别是谁(group_concat(name)
mysql> select count(name),group_concat(name) from cs where salary=2200;
案例:如何筛选出性别为男的员工,并分组
select count(name),group_concat(name) from cs where sex=‘male’;
GROUP BY和GROUP_CONCAT()函数一起使用
GROUP_CONCAT()-------组连接
mysql> select dep_id,group_concat(name) from cs group by dep_id;
#以dep_id分的组,dep_id这个组里面都有谁
#给组连接设置了一个别名
mysql> select dep_id,group_concat(name) as ‘xingming’ from cs group by dep_id;
各种函数
12.函数
max() 最大值
:mysql> select max(salary) from cs5;
查询薪水最高的人的详细信息:
mysql> select name,sex,hire_date,post,salary,dep_id from employee5 where salary = (SELECT MAX(salary) from employee5);
min() 最小值
select min(salary) from cs;
avg() 平均值
select avg(salary) from cs;
now() 现在的时间
select now();
sum() 计算和
select sum(salary) from cs where post=‘sale’;