---
title: "mysql查询语句"
author: "smallearth"
date: "2013-02-26T20:21:00Z"
tags: []
slug: "mysql"
draft: false
lastmod: "2013-11-30T20:10:21Z"
id: 1983283
url: "https://smallearth.iteye.com/blog/1983283"
---

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&lt;10;

例4.查询商品价格小于等于10美元的商品名称及价格

select prod\_name,prod\_price

from products

where prod\_price&lt;=10;

符号说明

=：等于

&lt; &gt;：不等于

！=：不等于

&lt; ,&lt;=： 小于，小于等于

Between..and：值与值之间

&gt;，&gt;=：大于，大于等于

例5.查询不是有供应商1003制造的所有产品。

select vend\_id,prod\_name

from products

where vendid&lt;&gt;1003;

注：&lt;&gt;和！=功能相同

例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&lt;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&gt;=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\\\\.]’

注意：^两种用法如果在集合中例如\[ ]表示否定；在集合前表示文本开始。

定位元字符

^：文本的开始

$：文本的结尾

\[\[:&lt;:]]：词的开始

\[\[:&lt;:]]：词的结尾