2.select prod_name from products limit 5,5
返回从第五行开始的五行。
3.select prod_id,prod_price,prod_name from products order by prod_price,prod_name;
首先按价格,再按名称排序
3.select prod_id,prod_price,prod_name from products order by prod_price desc
按价格降序排序
4.select prod_id,prod_price,prod_name from products order by prod_price desc,prod_name 先按价格降序排序
然后再对产品名排序,如果想在多个列上进行降序排序,必须对每个列指定desc关键字。
5.使用order by与limit组合,能够找出一个列中最高或最低的值
找出最昂贵的物品
select prod_price from products order by prod_price desc limit 1;
select prod_name,prod_price from products where prod_price=2.50
between 开始值和结束值
select prod_name, prod_price from products where prod_price between 5 and 10
null 无值
select prod_name from products where prod_price is null;
like操作符
 %匹配任意个字符
 select prod_name,prod_price from products where products where prod_name like ‘jet%’
 以jet开头
 select prod_name,prod_price from products where products where prod_name like ‘%jet’
 以jet结尾
 select prod_name,prod_price from products where products where prod_name like ‘%jet%’
 任何包含jet的
 select prod_name,prod_price from products where products where prod_name like ‘s%e’
 以s开头,以e结尾
 下划线匹配单个字符(一定是单个字符)
 select prod_name,prod_price from products where products where prod_name like ‘s_e’
 以s开头,以e结尾,中间一个单词,sae
 通配符使用技巧
 1.不能过度使用通配符,如果其他操作符能达到相同的目的,应使用其他操作符
 2.在确实需要使用通配符时,除非绝对有必要,否则不要把他们用在搜索的开始处。把通配符用于搜索的开始处是最慢的。
 正则表达式
 检索含文本1000的所有的行
 select prod_name,prod_price from products where products where prod_name regexp ‘1000’
 order by prod_name
 检索含文本1000的所有的行
 select prod_name,prod_price from products where products where prod_name regexp ‘.000’
 order by prod_name
 匹配含文本1000、2000、3000这些
 Or匹配
 select prod_name,prod_price from products where products where prod_name regexp ‘1000|2000’ order by prod_name
 匹配几个字符之一
 select prod_name,prod_price from products where products where prod_name regexp ‘[123]ton’ order by prod_name  匹配1 ton或者2 ton或者 3 ton
 匹配范围
 select prod_name,prod_price from products where products where prod_name regexp ‘[1-5]ton’
 order by prod_name
匹配特殊字符,为了匹配特殊字符,必须用\\为前导 \\-表示查找-, \\.表示查找.
 Select vend_name from vendors where vend_name regexp ‘\\.’ Order by vend_name
 使用数据处理函数
 Upper()将文本转换成大写
 left()返回串左边的字符
 Length()返回串的长度
 Locate()返回串的一个子串
 Lower()将串转换成小写
 lTrim()去掉串左边的空格
 Right()返回串右边的字符
 Rtrim()去掉串右边的空格
 soundex()返回串的soundex值
 substring() 返回子串的字符
以下是soundex()的使用实例
时间函数
如下例
如果你仅仅想要日期,则使用Date()是一个很好的习惯,因为可能是‘2005-09-01 11:30:30;
更可靠的输入为
聚集函数用来汇总数据
聚集不同的值可以用distinct
组合聚集函数
数据分组    group by      
过滤分组having
列出至少有两个订单的所有顾客
where在分组前进行过滤,having在分组后进行过滤。
where字句过滤掉所有prod_price小于10的数据,然后再按vend_id进行分组,然后过滤掉分组数小于2的
select子句的顺序
select   from  where(行级过滤)    group by(分组)     having(组级过滤)     order by ( 输出排序顺序) limit(要检索的行数)
子查询:即嵌套在其他查询中的查询,如下例子
1 查询每门课程成绩都大于80分学生的学号
 数据库 表 student
 name score course
 A 85 
语文
 A 75  数学
 A 82  英语
 B   75  语文
 B   89  数学
 B   79  英语
 天使美眉90 语文
 天使美眉100 数学
 天使美眉100 英语
 请找出每门课程都超过80分的那个人名字的SQL语句
2.select name from student group by name having name not in(select distinct name from student where score<= 80)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MySQL连表update
设想两张表,如下
table A
field id
field name
table B
field id
filed my_name
现在希望将表B中的my_name中的内容“拷贝”到表A中对应的name字段中,表A中的记录与表B中的记录通过id对应。首先我们来连表查询一下:
SELECT a.id, a.name, b.my_name
FROM A a, B b
WHERE a.id = b.id 根据连表查询,可以和容易的转为连表Update,思路类似,如下:
UPDATE A a, B b
SET a.name = b.my_name
WHERE a.id = b.id 
京公网安备 11010502036488号