mysql数据结合使用_MySQL数据行操作
增:
insert into 表名 (列名1, 列名2,) values(值1, 值2);
insert into 表名 (列名1, 列名2,) values(值1, 值2),(值1,值2),(值n,值n);
insert into 表名 (列名1, 列名2,) select 列名1, 列名2 from 表名;
删除:
delete from 表名;
delete from 表名 where id > 10
delete from 表名 where id < 10
delete from 表名 where id <= 10
delete from 表名 where id >= 10
delete from 表名 where id != 10
delete from 表名 where id = 10 and name='xxx'; and : 并且 两个条件都必须要成立
delete from 表名 where id = 10 or name='xxx'; or : 或者 只要满足一个条件成立
修改:
update 表名 set name='zekai', age=23 where id > 10;
查询:
基本:
select * from 表名;
select name , age from 表名;
高级:
a. where 条件查询:
select * from 表名 where id=10;
select * from 表名 where id >10 and id<15;
select * from 表名 where id > 10;
!= : 不等于
between and: between是一个闭区间
select * from t4 where id between 9 and 12;
in: 在某一个集合中
select * from t4 where id in (9,10,11....);
select * from t4 where id in (select id from t3 where id between 2 and 4)
b. 通配符:
alex
select * from 表 where name like 'ale%' - ale开头的所有(多个字符串)
select * from 表 where name like 'ale_' - ale开头的所有(一个字符)
c. 限制取几条:
select * from 表名 limit 索引偏移量, 取出多少条数据;
select * from t3 limit 0, 10; 第一页
select * from t3 limit 10, 10; 第二页
page = input('page:')
page 索引偏移量 数据量(offset)
1 0 10
2 10 10
3 20 10
4 30 10
page (page-1)*offset offset
分页核心SQL:
select * from t3 limit (page-1)*offset, offset;
d. 排序:
order by
降序:
select * from t4 order by 列名 desc; descending
升序:
select * from t4 order by 列名 asc; ascending
多列:
create table t7(
id int auto_increment primary key,
num int not null default 0,
age int not null default 0
)charset=utf8;
insert into t7 (num, age) values (2, 12),(3,13),(4, 12);
select * from t4 order by num desc, name asc;
如果前一列的值相等的话, 会按照后一列的值进行进一步的排序.
e. 分组
select age, 聚合函数(count(num)/sum(num)/max(num)/min(num)/avg(num)) from 表名 group by 列名;
select age, avg(num) from t7 group by age;
select age, count(num) from t7 group by age;
select age, count(num) as cnt from t7 group by age; 显示别名 as
having的二次删选:
select age, count(num) as cnt from t7 group by age having cnt>1;
where 和 having的区别:
1). having与where类似,可筛选数据
2). where针对表中的列发挥作用,查询数据
3). having针对查询结果中的列发挥作用,二次筛选数据, 和group by配合使用
f. 连表操作
select * from userinfo, department; (笛卡尔积)
select * from userinfo, department where userinfo.depart_id=department.id;
左连接:
select * from userinfo left join department on userinfo.depart_id=department.id;
左边的表全部显示, 右边没有用到不显示
右连接:
select * from userinfo right join department on userinfo.depart_id=department.id;
右边的表全部显示, 左边没关联的用null表示
内连接:
左右两边的数据都会显示
ps:
a.只需要记住左连接 left join
b.可以连接多张表 通过某一个特定的条件
注意查询的顺序:
select name,sum(score) from 表 where id > 10 group by score having age> 12 order by age desc limit 2, 10
mysql数据结合使用_MySQL数据行操作相关推荐
- mysql count if 去重_MYSQL数据去重
我们用的数据库是mysql,偶尔会因为程序处理上的方便或者sql未优化而增加了一些重复数据,最后需要对这些重复的数据进行删除 对于数据量不大的时候我一般用not in的方式来处理,或者删全表,导出不重 ...
- mysql状态监听_Mysql数据状态监听探讨(攒人品)_MySQL
bitsCN.com 年会前攒人品,你懂得! 应用场景: 长时间运行程序,需要几乎整表查询Mysql,还得在可容忍范围内响应数据变化. 方案一:通过Mysql自带的表更新时间 查询方案:SELECT ...
- mysql 的文件介绍_mysql 数据文件介绍
MyISAM在磁盘上存储成三个文件.第一个文件的名字以表的名字开始,扩展名指出文件类型..frm文件存储表定义.数据文件的扩展名为.MYD (MYData).索引文件的扩展名是.MYI (MYInde ...
- mysql查看表描述_MySQL表记录操作介绍(重点介绍查询操作)
MySQL表记录操作指的是对数据库表中数据进行CRUD增删改查操作,一下将一一给大家介绍,重点介绍查询操作. 一.插入数据(INSERT) 二.删除数据(DELETE) 三.修改数据(UPDATE) ...
- MYSQL数据库字段关联_MySQL数据库基础——操作关系表、连接查询
MySQL数据库基础入门--day11 一.操作关联表 1.关联关系: 关联关系有:多对一.多对多和一对多. 一对一关联关系可以应用在以下几方面: (1)分割具有很多列的表 (2)由于安全原因而隔离表 ...
- mysql事物开始命令_MySQL命令行事务
mysql中,事务是一些数据库操作语句,有3个特点: 1.只有使用了InnoD引擎的数据库或者表支持事务 2.事务可以用来维护数据库完整性,保证成批的sql语句要么全执行,要么全不执行,不会出现部分执 ...
- mysql 导出指定分隔符_mysql 导出行数据到txt文件,指定字符分割
select id,name, concat('tel:',phone) from user order by time INTO outfile 'user.txt' FIELDS terminat ...
- mysql导入数据io异常_mysql 数据同步 出现Slave_IO_Running:No问题的解决方法小结
mysql replication 中slave机器上有两个关键的进程,死一个都不行,一个是slave_sql_running,一个是Slave_IO_Running,一个负责与主机的io通信,一个负 ...
- mysql取出数据外键_mysql数据表有外键,应注意的几点(目前学习所获得的经验)...
mysql : 1.有做外键的表子表和父表的引擎要一致才能添加数据:否则报外键约束错误 2.数据 类型要一致,不然添加补了 外键: 3.InnoDB和 myisam的区别(来源:PHP100中文网) ...
最新文章
- Qt Creator将QML模块与插件一起使用
- aws创建html网页,AWS: 在AWS上创建一个网站,综合运用(Lambda + Api Gateway + Dynamodb + S3)...
- python进程的回收—wait
- 异常:Get请求提交过来的中文参数乱码
- debian 安装java_debian9安装jdk1.8
- Android Picasso最详细的使用指南
- 安卓学习笔记:使用PopupWindow创建简单菜单
- 【华为云技术分享】初识MySQL隔离级别
- 今天遇到了一个很坑爹的问题....
- Unity4.3 2D新特性
- ibm服务器网络安装系统安装系统安装教程,IBM服务器系统安装步骤.doc
- EtherCAT xml 解析(不定时更新)
- xp系统禁止开机启动服务器,Window XP 开机启动超慢,哪些系统服务和进程可以禁用?...
- 设计模式(十四)中介者模式
- oracle数据库 日期函数用法
- 格式化什么意思?格式化了数据还能恢复吗?
- iphone7运行linux,我在iPhone7装入了Linux系统!
- VMware设置共享文件夹之后Ubuntu中看不到怎么办?
- 第七周--数据结构--队列数组
- Python 金融量化 均线系统交易策略专题(简单移动平均,加权移动平均,指数加权移动平均,异同移动平均MACD等解读与绘图)