mysql单表数据列_MySQL 之 数据操作及单表查询
1、数据操作
(1)insert 增加数据
1. 插入完整数据(顺序插入)
语法一:
INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n);
语法二:
INSERT INTO 表名 VALUES (值1,值2,值3…值n);
2. 指定字段插入数据
语法:
INSERT INTO 表名(字段1,字段2,字段3…) VALUES (值1,值2,值3…);
3. 插入多条记录
语法:
INSERT INTO 表名 VALUES (值1,值2,值3…值n), (值1,值2,值3…值n), (值1,值2,值3…值n);
4. 插入查询结果
语法:
INSERT INTO 表名(字段1,字段2,字段3…字段n)
SELECT (字段1,字段2,字段3…字段n) FROM 表2
WHERE …;
(2)delete 删除数据
语法:
DELETE FROM 表名 WHERE CONITION;
示例:
DELETE FROM mysql.user
WHERE password=’’;
(3)update 修改数据
语法:
UPDATE 表名 SET 字段1=值1, 字段2=值2 WHERE CONDITION;
示例:
UPDATE mysql.user SET password=password(‘123’)
where user=’root’ and host=’localhost’;
(4)select 查询数据
语法
SELECT DISTINCT FROM 表名;
2、单表查询
单表查询的语法
SELECT DISTINCT 字段1,字段2... FROM 表名
WHERE 条件
GROUP BY field
HAVING 筛选
ORDER BY field
LIMIT 限制条数;
单表查询的关键字执行的优先级
from
where
group by
select
distinct
having
order by
limit
单表查询的执行过程
1.找到表:from
2.拿着where指定的约束条件,去文件/表中取出一条条记录
3.将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组
4.执行select(也可以distinct去重)
5.将分组的结果进行having过滤
6.将结果按条件排序:order by
7.限制结果的显示条数
(1)前期建数据库和表
# 建表和数据准备
# 创建表
create table employee(
id int not null unique auto_increment,
emp_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
);
# 插入记录
insert into employee(emp_name,sex,age,hire_date,post,salary,office,depart_id) values
#以下是教学部
('cai','male',78,'20150302','teacher',1000000.31,401,1),
('liu','male',81,'20130305','teacher',8300,401,1),
('ling','male',73,'20140701','teacher',3500,401,1),
('yongliang','male',28,'20121101','teacher',2100,401,1),
('marry','female',18,'20110211','teacher',9000,401,1),
('tom','male',18,'19000301','teacher',30000,401,1),
('liangliang','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)
;
(2)简单查询
# 查看表中所有
select * from 表;
# 指定列查询
select emp_name,salary from employee;
# 在列中使用四则运算
select emp_name,salary*12 from employee;
# 重命名
select emp_name,salary*12 as annul_salary from employee;
select emp_name,salary*12 annul_salary from employee;
# 去重 distinct
select distinct post from employee;
select distinct sex,post from employee;
# 定义显示格式
concat()函数 # 用于拼接字符串
select concat('姓名 :',emp_name),concat('年薪:',salary*12) from employee;
concat_ws()函数 第一个参数为分隔符
select concat_ws('|','a','b','c');
# 条件判断
case when语句 == if条件判断句
select
(
case
when emp_name = 'cai' then
emp_name
when emp_name = 'liu' then
CONCAT(emp_name,'_haha')
else
concat(emp_name, 'hehe')
end
) as new_name
from
employee;
(3)根据条件筛选行 where
# 比较运算 = > < >= <= !=/<>
select * from employee where age>18;
select * from employee where salary<10000;
select * from employee where salary=20000;
# between 在一个范围内; between a and b #[a,b]
select * from employee where salary between 10000 and 20000;
# in集合查询 ; in(80,90,100) 值是80或90或100
select * from employee where salary in (17000,19000);
# like 模糊查询
# _ 通配符 表示一个字符长度的任意内容
select * from employee where emp_name like 'c__';
# % 通配符 表示任意字符长度的任意内容
select * from employee where emp_name like 'c%';
select * from employee where emp_name like '%u';
select * from employee where emp_name like '%a%';
# regexp 正则匹配
select * from employee where emp_name regexp '^cai';
(4)逻辑运算 and or not
# and
select * from employee where age>18 and post='teacher';
# or
select * from employee where salary<10000 or salary>30000;
# not
select * from employee where salary not in (10000,17000,18000);
(5)关于 null
# 关键字IS NULL(判断某个字段是否为NULL不能用等号,需要用IS)
# 查看岗位描述为NULL的员工信息
select * from employee where post_comment is null;
# 查看岗位描述不为NULL的员工信息
select * from employee where post_comment is not null;
(6)5个聚合函数
强调:聚合函数聚合的是组的内容,若是没有分组,则默认一组
count # 统计次数
max # 求最大值
min # 求最小值
avg # 求平均值
sum # 求和
(7)分组聚合 group by
如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义
多条记录之间的某个字段值相同,该字段通常用来作为分组的依据
# 单独使用group by关键字分组
select post from employee group by post;
#注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数
#group by关键字和group_concat()函数一起使用
#按照岗位分组,并查看组内成员名
select post,group_concat(emp_name) from employee group by post;
select post,group_concat(emp_name) as emp_members from employee group by post;
# group by与聚合函数一起使用
#按照岗位分组,并查看每个组有多少人
select post,count(id) as count from employee group by post;
# 查询各部门年龄在20岁以上的人的平均薪资
select post,avg(salary) from employee where age>20 group by post;
(8)过滤 having (group by + 聚合函数)
# having与where不一样的地方在于:
执行优先级从高到低:where > group by > having
1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。
2. having发生在分组group by之后,因而having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数
# 查询平均薪资大于1w的部门
select avg(salary) from employee group by post having avg(salary) > 10000;
# 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
select post,group_concat(emp_name),count(id) from employee group by post having count(id)<2;
# 查询各岗位平均薪资大于10000的岗位名、平均工资
select post,avg(salary) from employee group by post having avg(salary) > 10000;
# 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000;
(9)排序 order by
# 升序 asc (默认升序)
select * from employee order by salary;
select * from employee order by salary asc;
# 降序 desc
select * from employee order by salary desc;
# 按多列排序:先按照age排序,如果年纪相同,则按照薪资排序
select * from employee order by age,salary;
select * from employee order by age,salary desc;
select * from employee order by age desc,salary;
(10)限制查询的记录数 limit
# 取前n条
select * from 表 order by 列 limit n;
# 从m+1开始,取n条
select * from 表 order by 列 limit m,n;
# 从m+1开始,取n条
select * from 表 order by 列 limit n offset m;
小结:select * from 表 where 条件 group by 分组 having 过滤 order by 排序 limit n;
3、在命令提示符窗口中遇到的编码问题解决方法:
1.临时解决问题 在客户端执行 set xxxx = utf8;
2.永久解决问题 在my.ini添加 set xxxx = utf8;
3.实时解决问题 create table 表名() charset=utf8;
mysql单表数据列_MySQL 之 数据操作及单表查询相关推荐
- R语言vtreat包自动处理dataframe的缺失值、使用分组的中位数来标准化数据列中每个数据的值(和中位数表连接并基于中位数进行数据标化)、计算数据列的中位数或者均值并进行数据标准化
R语言vtreat包自动处理dataframe的缺失值.使用分组的中位数来标准化数据列中每个数据的值(和中位数表连接并基于中位数进行数据标化).计算数据列的中位数或者均值并基于中位数或者均值进行数据标 ...
- mysql 全文本检索的列_排序数据列以检索MySQL中的最大文本值
为此,您可以将ORDER BY与一些聚合函数right()一起使用.让我们首先创建一个表-create table DemoTable1487 -> ( -> StudentCode te ...
- R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行YeoJohnson变换(将非正态分布数据列转换为正态分布数据、可以处理负数)、设置参数为YeoJohnson
R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行YeoJohnson变换(将非正态分布数据列转换为正态分布数据.可以处理负数).设置method参数为YeoJohns ...
- R语言dplyr包数据列重排(reorder)实战:把特定数据列移动到第一列、把特定数据列移动到最后一列、数据列多列重排、按照字母顺序重排数据列、把数据列反序
R语言dplyr包数据列重排(reorder)实战:把特定数据列移动到第一列.把特定数据列移动到最后一列.数据列多列重排.按照字母顺序重排数据列.把数据列反序 目录
- pandas使用query函数查询dataframe中某一个数据列在指定数据范围的数据行(rows where value is between two values in dataframe)
pandas使用query函数查询dataframe中某一个数据列在指定数据范围的数据行(rows where value is between two values in dataframe) 目录
- SQL查询库、表,列等的一些操作
SQL查询库.表,列等的一些操作 转载于:https://www.cnblogs.com/yangleikingly/p/6497499.html
- mysql数据表添加数值_MySQL中数据表和数据的操作
一.数据库表的操作 1.数据表的创建 mysql> create tablet_user(-> id int unsigned not null auto_increment primar ...
- mysql怎样查表的模式_mysql常用基础操作语法(四)--对数据的简单无条件查询及库和表查询【命令行模式】...
1.mysql简单的查询:select 字段1,字段2... from tablename; 如果字段那里写一个*,代表查询所有的字段,等同于指定出所有的字段名,因此如果要查询所有字段的数据,一般都是 ...
- mysql删除emp表的语句_MySQL删除数据表(DORP TABLE语句)
在MySQL数据库中,对于不再需要的数据表,我们可以将其从数据库中删除. 在删除表的同时,表的结构和表中所有的数据都会被删除,因此在删除数据表之前最好先备份,以免造成无法挽回的损失. 下面我们来了解一 ...
最新文章
- 经典JavaScript正则表达式实战
- 瓜子二手车CEO杨浩涌:创业要建立势能,瓜子的技术能力是护城河,“瓜子大脑”能预测成交概率...
- trap in development
- 论文多次被拒怎么办?Best Paper Award获得者聊聊如何才能中顶会
- apache mediawiki 安装_如何在CentOS 7上安装MediaWiki
- 论文浅尝 | HEAD-QA: 一个面向复杂推理的医疗保健数据集
- RDD之四:Value型Transformation算子
- python常用模块用法_python笔记之常用模块用法分析
- “搞机器学习没前途”
- DotNetBar TreeGx用法
- 计算机信息安全技术分为两个层次,计算机信息安全技术分为两个层次,其中的第二层次为()...
- 求不规则图形外接圆的算法 (附:三角形外接圆计算公式)
- 古典概型几何概型伯努利概型条件概率
- 正则表达式贪婪性---点星(.*) 20190618
- GPU与GPGPU泛淡
- vim 单行删除与多行删除
- pytorch应用之——纸币识别(一)
- 一份DevOps工程师职责清单,待你查阅
- 企业集成平台 Cloud Hub 5.3版本发布 [EAI、B2BI、EDI、数据集成平台]
- echarts绘制地图-china.json
热门文章
- 职称计算机execl试题,2017年职称计算机考试excel测试题
- IOS---如何获取当前的日期和时间(阴历阳历),并显示带阴历阳历24节气的日历
- DeviceDriver(十二):I2C驱动
- 低代码开发助力家电行业快速搭建售后服务工单管理系统-中易YiTS系统
- 2020张宇1000题【好题收集】【第三章:一元函数积分学】
- 利用 numpy 实现物理计算--物理向量符号与numpy数组的对应 ( jupyterlab 例子)
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.zyh.springboot.
- 测试你的逻辑推理能力
- 华为交换机ip地址与MAC地址绑定(全局/接口模式)
- Java如何跳出for双层循环