Mysql 查询语句<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.查询商品价格在510元之间商品。

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

注:NULL0及空字符串不同

 

9.where 子句(复合条件查询)

 
1.查询供应商编号为1003并且价格少于10美元商品

select prod_id,prod_name,prod_price

from products

where prod_id=1003 and prod_price<10

 
2. 查询供应商编号为10021003的商品

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开头的产品

select  prod_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’

 
likeregexp的区别在于如果不使用通配符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匹配

搜索两个串之一(也可以是多个)

查询产品名称中包含10002000的产品

select prod_id,prod_name

from products

where prod_name regexp 1000|2000

 
4.匹配几个字符之一

查询产品名称含有123 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]

查询产品名称中含有12345 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 030127

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个或1s匹配

                             重复元字符

*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\\.]

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

                         定位元字符

^:文本的开始

$:文本的结尾

[[:<:]]:词的开始

[[:<:]]:词的结尾