本篇文章基于《MYSQL必知必会》
1.了解MYSQL
数据库,表,列,类型,主键:唯一区分表中的各行
2.MYSQL简介
略
3.使用MYSQL
show databases; use database; select database(); show tables; describe table;
4.检索数据
select * from table limit 1;
可以用这个检索第一条数据
5.排序数据
select * from table order by name; 用名字来进行排序
select * from table order by name DESC; 用降序排列,默认的是升序
select * from table order by name DESC limit1;查找出名字排在第一的
6.过滤数据
selecr * from table where name= 'allen'; 查找出名字是allen的对应数据
类似的可以用操作符如(<,!=)来进行数据的过滤
selecr * from table where name IS NULL; 空值检查
7.数据过滤
用AND,OR来进行操作,注意AND的优先级要高于OR
IN(1000,1111)此条可以用OR代替
NOT 否定
8.用通配符过滤
like
% 表示任何字符出现任意次数,但不能通配NULL
_ 通配一个字符
另外注意不要过度使用通配符而导致出现不需要的数据
9.正则表达式进行搜索
select * from table whre name regexp 'allen';
like 和regexp的区别在于like是完全匹配,而regexp是部分匹配
可以用|=or来匹配多个
匹配多个字符之一【123】
特殊字符要转义\\.
匹配多个如‘【0-9】{n}’
定位符^,$
10.创建计算单元
select concat(column1, '(,'column2'),') from table;利用concat连接俩个列
select concat(column1, '(,'column2'),') AS title from tables;利用AS来创建别名
可以利用+-*/来运算
11.实用数据处理函数
文本处理函数如UPPER,RTTIM
时期日期处理函数date
数字处理如abs()
12.汇总函数
一些聚集函数count,max,min,avg,sum
13.分组数据
select * from table group by xxx;对信息进行分组
select * from table group by xxx having >=xxx;进行分组过滤
注意实用order by 来组合进行排序
14.使用子查询
select * from table where index in (select * from table2(select...));
15&16 联结表与高级联结
此俩章较难,先弄清概念:内外联结的区别是内联结将去除所有不符合条件的记录,而外联结则保留其中部分。外左联结与外右联结的区别在于如果用A左联结B则A中所有记录都会保留在结果中,此时B中只有符合联结条件的记录,而右联结相反。
inner join ...on
right(left) outer join ...on
17.组合查询
俩个或多个的SELECT查询可以使用组合查询
select index1,index2,index3 from tables where id<=5 union all select index1,index2,index3 from tables where name='allen';
18.全文本搜索
只有MYISAM引擎可用,搜索前要检索
select xxx from table where match(xxx) against(‘allen'); 查找xxx列中包含allen
扩展 select xxx from table where match(xxx) against('allen' with query expansion); 可以查找不含allen但含allen那行其它的单词,如allen那行有bill,则列出有bill的行
布尔搜索 select xxx from table where match(xxx) against('allen' in boolean mode); 可以利用布尔操作符来限定。 -,+,<,>