mysql再探
select子句及其顺序
select from where group by having order by limit
创建表
create table student(id int not null auto_increment,name varchar(20) default 'noname',age int,primary key(id)) engine=innodb;主键必须唯一,而且主键可以为多个,例如primary(id,name),auto_increment自增,default默认值,engine引擎,引擎有innodb,可靠的事物处理引擎,不支持全文本搜索,memory
数据存储在内存不是磁盘,速度快,适合临时表,myisam性能极高的引擎,支持全文本搜索,但不支持事物处理
插入数据
insert into student values(123,'linux',22);
insert into student values(123,'linux',22),(11,'test',23);同时插入两条数据
insert into student(id ,name,age) values(123,'linux',22);
insert into student(id ,name) values(123,'linux');
insert into student(id ,name,age) select id_cp,name_cp,age_cp
from student_cp;插入检索出的数据
更新数据
update student set id=100 where name='test';
update student set id=NULL where name='test';
update student set id=100,age=22 where name='test';同时修改id和age
删除数据
delete from student where name='test';删除名字为test的那行
更新表
alter table student add addr char(20);给student表增加一个地址列
alter table student drop column addr;删除student表地址列
定义外键
alter table orderitems add constraint fk_orderitems_orders foreign key (order_num) references orders (order_num); 其中constraint命名外键为fk-orderitems_orders,对应外键为order_num,链接的外表列为order_num
删除表
drop table student;
重命名表
rename table student to stuinfo;重命名为stuinfo
rename table student to stuinfo,orders to dingdan;同时重命名两个表
正则表达式的使用
select * from student where name regexp 'bp' order by id;找出名字包含bp的所有行
select * from student where name regexp 'bp.' ; 名字为bp且后面跟一个字符的学生,如果需要区分大小写,只需要在regexp后面加关键字binary
select * from student where name regexp 'bp|bp1|bp2';列出名字为bp或者bp1和bp2的
select * from student where name regexp 'bp[12]';列出叫做bp1以及bp2的学生,改成bp[1|2]也是一样的意思
select * from student where name regexp 'bp[^12]';列出不叫做bp1以及bp2的学生
select * from student where name
regexp 'bp[1-9]';列出叫做bp1,bp2,bp3...的学生
select * from student where name regexp 'bp[a-z]';列出叫做bpa,bpb,bpc...的学生
匹配特殊字符
select * from student where name
regexp '\\-';列出包含字符-的学生,\\用来转义
匹配字符类
[:alnum:] 任意字符和数字[a-zA-Z0-9]
[:alpha:] 任意字符[a-zA-Z]
[:blank:] 空格和制表[\\t]
[:cntrl:] ASCII控制字符,ASCII 0-31和127
[:digit:] 任意数字[0-9]
[:graph:] 与[:print:]相同,但不包括空格
[:lower:] 任意小写字母[a-z]
[:upper:] 任意大写字母[A-Z]
[:print:] 任意可打印字符
[:punct:] 既不在[:alnum:]又不在[:cntrl:]中的任意字符
[:xdigit:] 任意十六进制数字[a-fA-F0-o]
[:space:] 包括空格在内的任意空白字符[\\f\\n\\r\\t\\v]
匹配多个实例
* 0个或多个匹配
+ 1个或多个匹配
? 0个或1个匹配
{n} 指定数目匹配
{n,} 大于或等于n次的匹配
{n,m} 匹配数目为n到m
select * from student where name regexp '\\([0-9] bp?\\)';名字包含(1 bpa)之类的
select * from student where name
regexp '[[:digit:]]{4}';名字包含4个数字的学生
定位符
^ 文本的开始
$ 文本的结束
[[:<:]] 词的开始
[[:>:]] 词的结束
select * from student where id regexp '^[0-9\\.]';等价'^[0-9|\\.]即数字开头或.开头,注意区分,如果^在[ ]里面,是否定该集合的意思,在[ ]外面就是开头的意思
select concat(id,name,age) from student;把id和name以及age连起来成为一个字符串
select trim(name)
from student;去掉name左右的空格后输出,ltrim去掉左边空格,rtrim去掉右边空格
select
concat(id,name,age) as linux from student;把列名改为linux
select price,counts,price*counts
as money from produces;增加一行统计价格并命名为money
select upper(name)
as linux from student;将名字转换为大写,lower()小写,left()返回串左边的字符,right()返回串右边的字符,locate()找出串的一个子串,soundex()返回串的soundex值,即发音差不多的,如lee和lie等等。
时间和日期处理函数
adddate()
增加一个日期
addtime()
增加一个时间
curdate()
返回当前时间
curtime()
返回当前时间
date()
返回日期时间的日期部分
datediff()
计算两个日期之差
day() hour()
minute() month() now() second() year() 返回对应时间
select * from orders
where date(order_date)='2017-5-1' ;如果只知道查找某一天的,但是订单里的日期有包括了具体时刻,那么将其转换为date格式,就能找到相应日期的订单了
select
* from orders where date(order_date)='2017-5-1' and '2017-5-3';订单在该范围
select
* from orders where year(order_date)=2017 and month(order_date)=4;年月
数值处理函数
abs 绝对值
cos 余弦值
exp 指数值
mod 余数
pi 圆周率
rand 随机数
sin 正弦值
sqrt 平凡根
tan 正切值
汇总数据
avg 某列的平均值
count 某列的行数
max 某列的最大值
min 某列的最小值
sum 某列值之和
select min(id) from student; 打印最小的id
select sum(price*counts)
as money from produces;统计总价
分组数据
select id,count(*)
from student group by id;根据id分组,并且统计每组id数据数量
select id,count(*)
from student group by id having count(*)>=2;打印id组数量大于2的,having相当跟where差不多,唯一的差别在于where过滤行,having过滤组
select
id,count(*) from student where id>=2 group by id having count(*)>=2;
group by 和 order by 的区别:
order by 产生有序的的输出,group by 分组行,但输出可能不是分组的顺序
select id,sum(id*age) from student group by id order by sum(id*age); 计算每个id组里面id*age的总和,再根据和排序
子查询
select cust_id from orders where order_num in(select order_num from orderitems where prod_id ='TNT2');
select cust_name,cust_state,(select count(*) from orders where orders.cust_id=customers.cust_id) as orders from customers order by cust_name;
select vend_name,prod_name,prod_price from vendors,products where vendors.vend_id=products.vend_id order by vend_name,prod_name
笛卡儿积(搜索结果行的数目是地一个表的行数乘以第二个表的行数)
select vend_name,prod_name,prod_price from vendors,products order by vend_name,prod_name;
内部联结
select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id=products.vend_id; 内部联结inner join on 联结条件用on来代替where
表别名
select a.id,a.name from student as a; 用a来代替student
左外连接
select customers.cust_id,orders.order_num from customers left outer join orders on customers.cust_id=orders.cust_id; 右外连接是right outer join on,其区别是左连接时,如果右边没有数据,那么只会显示左边的数据,右边显示null,右外连接则相反
联合查询
select vend_id,prod_id,prod_price from products where prod_price <=5 union select vend_id,prod_id,prod_price from products where vend_id in(1001,1002);把满足这两个条件的都列出来,而且会去掉重复的行,如果不想去掉重复的行,使用union all,注意union必须由两条或者两条以上的select
语句组成,且每个查询必须包含相同的列
全文本搜索
create table produce(note_id int auto_increment,prod_id char(10),note_date datetime,note_text text,primary key(note_id),fulltext(note_text));
select * from produce where match(note_text) against('linux');搜索note_text中包含linux的行,刚开始的时候因为赋值给prod_id时为了加'',导致没搜索出来,后来改过来后就可以了,使用like来搜索也有差不多的结果,不过like返回的结果不是根据字符串偏前面的先返回
布尔文本搜索
select note_text from produce where match(note_text) against('heavy -rope*' in boolean mode);只返回匹配heavy且不包含rope开头的字符串,+代表必须存在
select note_text from produce where match(note_text) against('heavy ropes' in boolean mode);至少包含heavy或者ropes
select note_text from produce where match(note_text) against('"heavy ropes"' in boolean mode);包含短语heavy ropes
select note_text from produce where match(note_text) against('>heavy <rope' in boolean mode);优先heavy 降低rope
select note_text from produce where match(note_text) against('+heavy +(<rope)' in boolean mode);两个都必须存在,后者的优先级降低
创建视图
create view stuinfo as select * from student where id>2 and id<10;
select * from stuinfo;就能查看id在2到10之间的学生信息了,而且还可以把某种固定格式的搜索结果格式转换为视图,方便使用
mysql再探的更多相关文章
- 【再探backbone 02】集合-Collection
前言 昨天我们一起学习了backbone的model,我个人对backbone的熟悉程度提高了,但是也发现一个严重的问题!!! 我平时压根没有用到model这块的东西,事实上我只用到了view,所以昨 ...
- ViewPager+Fragment再探:和TAB滑动条一起三者结合
Fragment前篇: <Android Fragment初探:静态Fragment组成Activity> ViewPager前篇: <Android ViewPager初探:让页面 ...
- 再探jQuery
再探jQuery 前言:在使用jQuery的时候发现一些知识点记得并不牢固,因此希望通过总结知识点加深对jQuery的应用,也希望和各位博友共同分享. jQuery是一个JavaScript库,它极大 ...
- [老老实实学WCF] 第五篇 再探通信--ClientBase
老老实实学WCF 第五篇 再探通信--ClientBase 在上一篇中,我们抛开了服务引用和元数据交换,在客户端中手动添加了元数据代码,并利用通道工厂ChannelFactory<>类创 ...
- Spark Streaming揭秘 Day7 再探Job Scheduler
Spark Streaming揭秘 Day7 再探Job Scheduler 今天,我们对Job Scheduler再进一步深入一下,对一些更加细节的源码进行分析. Job Scheduler启动 在 ...
- 再探ASP.NET 5(转载)
就在最近一段时间,微软又有大动作了,在IDE方面除了给我们发布了Viausl Studio 2013 社区版还发布了全新的Visual Studio 2015 Preview. Visual Stud ...
- 再探java基础——break和continue的用法
再探java基础——break和continue的用法 break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后 ...
- 第四节:SignalR灵魂所在Hub模型及再探聊天室样例
一. 整体介绍 本节:开始介绍SignalR另外一种通讯模型Hub(中心模型,或者叫集线器模型),它是一种RPC模式,允许客户端和服务器端各自自定义方法并且相互调用,对开发者来说相当友好. 该节包括的 ...
- 深入出不来nodejs源码-内置模块引入再探
我发现每次细看源码都能发现我之前写的一些东西是错误的,去改掉吧,又很不协调,不改吧,看着又脑阔疼…… 所以,这一节再探,是对之前一些说法的纠正,另外再缝缝补补一些新的内容. 错误在哪呢?在之前的初探中 ...
随机推荐
- 用Python的导入csv、文本文件、Excel文件的数据
使用read_csv函数导入CSV文件 read.csv函数语法 read_csv(file,encoding) 例子: Age,Name 22,wangwei 23,lixin 24,liqing ...
- 5.4 C++重载输入与输出操作符
参考:http://www.weixueyuan.net/view/6382.html 总结: 在C++中,系统已经对左移操作符“<<”和右移操作符“>>”分别进行了重载,使其 ...
- 3.8 C++继承机制下的析构函数
参考:http://www.weixueyuan.net/view/6365.html 总结: 构造函数的执行顺序是按照继承顺序自顶向下的,从基类到派生类,而析构函数的执行顺序是按照继承顺序自下向上, ...
- mysql创建存储过程,定时任务,定时删除log
-- 创建存储过程 清除30天前的日志create procedure deleteLog()BEGINdelete from contract_vlog where create_time<D ...
- <Parquet><Physical Properties><Best practice><With impala>
Parquet Parquet is a columnar storage format for Hadoop. Parquet is designed to make the advantages ...
- ubuntu安装scrapy方法
sudo apt-get install python-dev [默认安装python2] sudo apt-get install python3-dev [指定安装python3最新的] ...
- 2019-03-25-day018-面向对象
re模块 字符串匹配 列表 = findall(正则表达式,待匹配的字符串) 找所有 结果集 = search 找第一个,结果集.group() 结果集 = match 从第一个字符开始匹配,结果集. ...
- :after和:before伪元素的解释
:after 是清除浮动 让其高度和内容高度相同 :before 是防止上边塌陷 关注微信小程序
- SQL注入之Sqli-labs系列第二十八关(过滤空格、注释符、union select)和第二十八A关
开始挑战第二十八关(Trick with SELECT & UNION) 第二十八A关(Trick with SELECT & UNION) 0x1看看源代码 (1)与27关一样,只是 ...
- 设计简单算法体验Vivado HLS的使用
前言 本文主要讲解了使用Vivado HLS设计简单C语言的二选一选择器算法的硬件HLS开发的全流程,包括工程创建-算法验证和仿真-算法综合-RTL仿真-IP封装等步骤. 参考网站: http://b ...