MySql必知必会笔记

1.限制条件
select pro_name
from products
limit 5
limit 5 指示MySQL返回不多于5行
select pro_name
from products
limit 5,5指示MySQL返回从行5开始的5行 (limit 开始位置,行数)ps:存在行0

2.完全限定的表名
select products.pro_name
from products

假设products表位于crashhouse数据库中
select products.pro_name
from crashhouse.products

3.排序
select pro_name
from products
order by pro_name
默认升序排序

如需降序排序+desc
select pro_name
from products
order by pro_name desc

多列排序
select pro_id,pro_price,pro_name
from products
order by pro_price,pro_name

多列组合排序
select pro_id,pro_price,pro_name
from products
order by pro_price desc,pro_name
desc只应用到前面的列名,pro_price降序,pro_name默认升序

ASC表示升序,用处不大,因为排序默认即为升序

注意点.limit 与order by 结合,可找出一个列中最高项与最低项
select pro_name
from products
order by pro_name desc
limit 1;
显示列中最高值

4.过滤数据
where 用于限制搜索条件
①某个范围内的数据
select pro_name,pro_price
from products
where pro_price=2.5
MySql必知必会笔记_第1张图片
强调下between
select pro_name,pro_price
from products
where pro_price between 1.5 and 2.6 (between ...and... 在指定的两个值之间)

②检索单个值
select pro_name,pro_price
from products
where pro_name=‘xiaoming’ (加单引号是由于该字段为字符串,所以必须用单引号)

这里引申下有时可用like
select pro_name,pro_price
from products
where pro_name like ‘xiaoming’

③不匹配检查
select pro_name,pro_price
from products
where pro_price < > 2.5
select pro_name,pro_price
from products
where pro_price != 2.5

④空值检查
NULL 无值(no value),与字段包含0、空字符串或仅仅包含空格不同
select pro_name,pro_price
from products
where pro_price is NULL ;
返回没有价格的所有产品,不是价格为0的产品

⑤组合过滤
select pro_name,pro_price
from products
where pro_name=‘xiaoming’
and
pro_price = 2.5
--解释:and 表示两个条件都要满足

select pro_name,pro_price
from products
where pro_name=‘xiaoming’
or
pro_price = 2.5
--解释:or 表示满足任一条件即可

同时由于and在计算次序中优先级更高,如将and 与or结合使用时,要适当运用圆括号
select pro_name,pro_price
from products
where pro_name=‘xiaoming’
and
(pro_price =3.6
or
pro_price = 2.5)

⑥IN 操作符
select pro_name,pro_price
from products
where pro_name IN (‘xiaoming’,'xiaohong')
IN用来指定条件范围,只要满足的都能搜索出

⑦not操作符
select pro_name,pro_price
from products
where pro_name not IN (‘xiaoming’,'xiaohong')
not表示否定后面跟的条件,如上面语句表示的就是不选名字为xiaoming 与 xiaohong

5.通配符
like操作符
上面有写到like,当需要模糊匹配时,like就十分有用了。
结合%通配符
①找出所有xiao开头的产品
select pro_id,pro_name
from product
where pro_name like 'xiao%'
%可放在字符串任意位置,替代任意字符
%不能匹配NULL
_ 用法一样但只能匹配一个字符

PS:通配符会导致搜索的时间较精确搜索慢,所以不要过度使用通配符

6.用正则表达式搜索
正则表达式推荐学习链接: http://help.locoy.com/Document/Learn_Regex_For_30_Minutes.htm (正则表达式30分钟入门)

select pro_id,pro_name
from product
where pro_name regexp '1000'
regexp是regular express(正则表达式的缩写),它告诉MySQL:regexp后面跟的东西作为正则表达式

① . 表示匹配任意字符

②进行or匹配, 使用 |
select pro_name
from product
where pro_name regexp '1000|2000'
order by pro_name

③匹配几个字符之一
select pro_name
from product
where pro_name regexp '[123]Ton'
order by pro_name

[123]定义一组字符,它的意思是匹配1或2或3,后面又接了Ton,所以返回的是1Ton,2Ton,3Ton
由此可以看出[]是另一种形式的or语句
[^123]匹配除这些字符外的任意东西,^当然还有另一种用处,后面会讲到

④匹配范围
如想匹配[0123456789],想简化可写为[0-9]
当然不限于数值,[a-z]匹配任意字母字符

⑤匹配特殊字符
比如像 . |这种想要匹配,前面需要加\\,这种处理就是所谓的转义
select pro_name
from product
where pro_name regexp '\\|'
order by pro_name
MySql必知必会笔记_第2张图片
⑥匹配字符类
MySql必知必会笔记_第3张图片
⑦匹配多个实例
MySql必知必会笔记_第4张图片

例子:
打算匹配连在一起的4个数字
select pro_name
from product
where pro_name regexp '[:digit:]{4}'
order by pro_name
同时也可写为
select pro_name
from product
where pro_name regexp '[0-9][0-9][0-9][0-9]'
order by pro_name
写法不唯一

⑧定位符
MySql必知必会笔记_第5张图片
想找一个数(包括以小数点开始的数) 开始的产品,只搜索[0-9\\.] 或[:digit:]\\. 这种是不行的,因为它会在任意位置查找匹配
解决方法是使用^
select pro_name
from product
where pro_name regexp '^[0-9\\.]'
order by pro_name
^的双重用法
[^123]在集合中用来否定该集合,否则用来指串的开始处

小贴士:简单的正则表达式测试,在不适用数据库的情况下测试正则
select 'hello' from regexp '[0-9]';
显然会返回0,因为文本中无数字

7.















你可能感兴趣的:(SQL,数据分析)