mysql查询语句

Mysql 查询语句
1.显示products上的所有内容
select * from products;
2.查询products上的prod_name列
select prod_name from products;
3.查询products上的prod_name,prod_id,prod_price三列的值
select prod_id,prod_name,prod_price from products;
4.查看products上的vend_id供应商ID值并且去掉重复元组
select distinct vend_id from products;
5.限制显示结果limit
查看prod_name的前5行
select prod_name from products limit 5;
6.查看从行5开始的4条记录。(行是从0开始,行5其实就是从第六条开始显示)
select prod_name form products limit 5,4;
7.排序
order by子句默认升序asc,降序desc
例1.查看products上的prod_name按照prod_name升序排列
select prod_name from products order by prod_name
例2.多个列排序,先按前面的主序排列,主序相同再按照次序,以此类推。
select prod_id,prod_price,prod_name
from products
Order by prod_price,prod_name
例3.降序desc
select prod_id,prod_price,prod_name
from products
order by prod_price desc
8.where子句
例1.查询商品价格等于2.50的记录
select prod_name,prod_price from products where prod_price=2.50;
例2.查询商品名称为’fuses’的商品价格
select prod_name,
from products
where prod_name=‘fuses’;
例3.查询商品价格小于10美元的商品名称及价格
select prod_name,prod_price
from products
where prod_price<10;
例4.查询商品价格小于等于10美元的商品名称及价格
select prod_name,prod_price
from products
where prod_price<=10;
符号说明
=:等于
< >:不等于
!=:不等于
< ,<=: 小于,小于等于
Between..and:值与值之间
>,>=:大于,大于等于
例5.查询不是有供应商1003制造的所有产品。
select vend_id,prod_name
from products
where vendid<>1003;
注:<>和!=功能相同
例6.查询商品价格在5到10元之间商品。
select prod_name,prod_price from products
where prod_price between 5 and 10;
例7.NULL
查询商品价格为NULL的商品
select prod_name from products where prod_price is NULL
注:NULL和0及空字符串不同
9.where子句(复合条件查询)
例1.查询供应商编号为1003并且价格少于10美元商品
select prod_id,prod_name,prod_price
from products
where prod_id=1003 and prod_price<10
例2.查询供应商编号为1002或1003的商品
select prod_id,prod_name,prod_price
from products
where prod_id=1002 or prod_id=1003
例3.and or优先级
select prod_name,prod_price
from products
where vend_id=1002 or vend_id=1003 and prod_price>=10
10. in包含()中所有内容
例1.select prod_name,prod_price
from products
where vend_id in(1002,1003)
等价于
select prod_name,prod_price
from products
where vend_id =1002 or vend_id=1003
例2. not in不包括
select prod_name,prod_price
from products
where vend_id not in(1002,1003)
11. like通配符%(0个或表示任意多个字符)和_(只代表一个字符,不能代表0个字符)
例1.查询所有名称里包含jet开头的产品
selectprod_id,prod_name,prod_price
from products
where prod_name like‘jet%
例2.
select prod_id,prod_name
from products
where prod_name like‘_ton anvil’
不能代表0个字符
select prod_id,prod_name
from products
where prod_name like‘__ton anvil’
12.在查询中使用正则表达式
正则表达式是用来匹配文本的特殊字符串(字符集合)作用就是匹配文本字符串(必会)。
正则表达式能做什么?例如从一个文本文件中提取电话号码,想查找所有文件中带数字的文件,如果想替换一个页面中的所有URL为这些URL的实际HTML等都可以使用正则表达式。
在正则表达式regExp中.代表匹配任意字符
例1.查询prod_name字段中包含1000的产品
select prod_name from products
where prod_name regexp ‘1000’
对比
select prod_name from products
where prod_name like ‘1000’
like和regexp的区别在于如果不使用通配符like相当于=整个字段值而regexp会匹配字段内的内容
例2. select prod_name from products
where prod_name regexp‘.000
对比
select prod_name from products
where prod_name like‘_000
例3.进行or匹配
搜索两个串之一(也可以是多个)
查询产品名称中包含1000或2000的产品
select prod_id,prod_name
from products
where prod_name regexp‘1000|2000
例4.匹配几个字符之一
查询产品名称含有1或2或3 Ton的产品
select prod_name from products
where prod_name regexp‘[123] Ton
注:[123] Ton相当于[1|2|3] Ton,如果写成1 |2|3 Ton就成了查找产品名称带1的或者带2的或者带3Ton的
另外:^表示非[^123],是指除了123以外的
例5.匹配数字可以简写
[0123456789]可以简写成[0-9]
[12345]可以简写成为[1-5]
查询产品名称中含有1或2或3或4或5 Ton的产品
select prod_name from products
where prod_name regexp‘[1-5] Ton
例6.匹配特殊字符(.、|、[、])---转义字符\\
查询供应商名称中带.的供应商
错误(因为.是通配符,得进行转义变成普通字符)
select vend_name from vendors
where vend_name regexp‘.
正确
select vend_name from vendors
where vend_name regexp‘\\.
例7.匹配字符类(预定义字符集,就是已经定义好的规则)
1.[:alnum:]任意字母和数字(同[a-zA-Z0-9]
2.[:alpha:]任意字符(同[a-zA-Z]
3.[:blank:]空格和制表(同[\\t]
4.[:cntrl:]ASCII控制字符(ASCII 0到30和127)
5.[:digit:]任意数字(同[0-9]
6.[:graph:]与[:print:]相同,但不包括空格
7.[:lower:]任意小写字母(同[a-z]
8.[:print:]任意可打印字符
9.[:punct:]既不在[:alnum:]又不在[:cntrl:]中任意字符
10.[:space:]包括空格在内的任意空白字符(同[\\f\\n\\r\\t\\v]
11.[upper]任意大写字母(同[A-Z]
12.[:xdigit:]任意十六进制数字(同[a-fA-F0-9]
例8.1匹配多个实例
查询产品名称中包含数字stick和数字sticks的商品
select prod_name from products where prod_name regexp‘\\([0-9] sticks?\\)
s?代表0个或1个s匹配
重复元字符
*:0个或多个匹配
+:1个或多个匹配(等于{1,})
?:0个或1个匹配(等于0,1)
{n}:指定数目匹配;
{n,}:不少于指定数目的匹配;
{n,m}:匹配数目的范围(m不超过255)
例8.2匹配多个实例
查询产品名称包含连在一起的4位数字(前面学过任意数字可以是[:digit:]类,亦可以是[0-9])
select prod_name from products where prod_name regexp‘[[:digit:]]{4}
或prod_name regexp‘[0-9]{4}’
注:[:digit:]匹配任意数字,因而它为数字的一个集合。{4}确切的要求它前面的字符(任意数字)出现4次,所以[[:digit:]]{4}匹配连在一起的任意4位数字。
例9.定位符:前面的例子都是匹配一个字符串的任意位置文本,为了匹配特定位置的文本,就要使用定位符。
查询一个以数字或.开头的产品
select prod_name from products where prod_name regexp‘^[0-9\\.]
注意:^两种用法如果在集合中例如[ ]表示否定;在集合前表示文本开始。
定位元字符
^:文本的开始
$:文本的结尾
[[:<:]]:词的开始
[[:<:]]:词的结尾
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值