1、限制返回的行数:
(1)
SELECT author FROM book LIMIT 2;
该命令将返回前两行记录
(2)
SELECT author FROM book LIMIT 1, 2;
该命令返回两条记录,但是该记录是从第二条记录开始算起(注意,行数计数的起点是从0而不是1开始的,这跟Java中的数组很象)
2、排序返回的结果:(order by)
SELECT name, birth FROM pet ORDER BY birth;
返回的序列以升序(默认)形式排列,如果想降序排列,则需要这样:
SELECT name, birth FROM pet ORDER BY birth DESC;
你也可以显式指明按升序方式排列:
SELECT name, birth FROM pet ORDER BY birth ASC;
DESC(ASC)也可以应用与多个column中:
SELECT name, species, birth FROM pet ORDER BY species, birth DESC;
结果:
+----------+---------+------------+
| name | species | birth |
+----------+---------+------------+
| Chirpy | bird | 1998-09-11 |
| Whistler | bird | 1997-12-09 |
| Claws | cat | 1994-03-17 |
| Fluffy | cat | 1993-02-04 |//------------------
| Fang | dog | 1990-08-27 |// 降序排列
| Bowser | dog | 1989-08-31 |//
| Buffy | dog | 1989-05-13 |//
| Puffball | hamster | 1999-03-30 |//-------------------
| Slim | snake | 1996-04-29 |
+----------+---------+------------+
3、去掉返回结果中相同的记录(只返回一个):
SELECT DISTINCT amount FROM loan;
4、模糊查询:
SELECT name FROM author WHERE name LIKE 'M%';
MySQL有两个通配符 % 和 _ 。其中 % 匹配 0或多个任意的字符, _ 匹配一个 任意字符
5、获得某一行的最小值、最大值、平均值, 总和:
SELECT MIN(wt), AVG(wt), MAX(wt), SUM(wt) FROM person;
6、时间运算:
SELECT NOW(), CURTIME(), CURDATE();
运行结果:
+-----------------------+-----------+------------+
| NOW() | CURTIME() | CURDATE() |
+-----------------------+-----------+------------+
| 2007-09-14 10:42:20 | 10:42:20| 2007-09-14|
+-----------------------+-----------+------------+
7、把查询结果导出到文件:
SELECT * FROM author
INTO OUTFILE '/tmp/author'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
当用 into dumpfile 代替 into outfile时,select语句返回的结果必须不能多于1条。into dumpfile写入文件的格式既没有column分隔符,也没有行分隔符,这在将BLOB写入文件时很有用。
posted on 2007-09-14 10:45
Jeff Lee 阅读(176)
评论(0) 编辑 收藏 所属分类:
database