ITeye博客

MYSQL完全手册学习笔记(第九章)

qmug · 2008-10-21 17:51 · Mysql · 企业架构 · Markdown

使用数据

插入、更新 和删除记录

插入 Insert into

Describe 表名

当向表中插入字符串值(以及一些数据值)时,必须使用引号。不过数值不需要引号。

使用default 值

使用autoincrement 字段

使用unique字段

Replace ,命令

使用null 值

更新记录

Update set

删除记录

Delete

Mysql:>delete from users;

Musql:> truncate table users;

注意:reuncate 比delete 要快 因为truncate删除了表 然后重新建它,而delete删除的是记录,并没有尝试去修改表

Select where limit

Select t1,t2,t3 from t where a>300 and a <5000 ; 查询300 到5000之间的某些字段

Mysql:> select t1,t2,t3 from t a=’30’ or b=’r’ ; 查询出来 a=‘30 ’ 或者 b =r 的 字段信息

使用内建函数

Mysql:> select count(*) from a; Mysql:> select max(b) ,min(c) from test;

为表和列取别名 as Mysql:>select a as b, a2 as b2 from t; 给字段取得别名

Mysql:>select a ,b from t as haha; 给表取得别名

Mysql:> select a from limit 3,6 从第4行开始(注意是从第0行开始计数)开始返回6条记录

对查询结果排序

Order by
子句指定的用逗号隔开的多个字段进行排序 asc 和desc

mysql> select * from chengji; +----+------+------+---------+------------+ | id | name | math | physics | literature | +----+------+------+---------+------------+ | 1 | john | 60 | 37 | 45 | | 2 | jim | 96 | 89 | 92 | | 3 | bill | 65 | 12 | 57 | | 4 | hary | 69 | 85 | 12 | +----+------+------+---------+------------+ 4 rows in set (0.09 sec)

mysql> select @oldset := min(math) from chengji; +----------------------+ | @oldset := min(math) | +----------------------+ | 60 | +----------------------+ 1 row in set (0.00 sec)

mysql> select @oldset; +---------+ | @oldset | +---------+ | 60 | +---------+ 1 row in set (0.00 sec)

选择自己想要的

使用子查询

mysql> select math from chengji where math> (select avg(math) from chengji); +------+ | math | +------+ | 96 | +------+ 1 row in set (0.02 sec)

mysql> select math from chengji where math< (select avg(math) from chengji); +------+ | math | +------+ | 60 | | 65 | | 69 | +------+ 3 rows in set (0.02 sec)

控制select 行为

例如 distinct 等、

204P

复制、倒入和导出记录

复制记录

Load data infile

mysql> select math,physics,literature from chengji into outfile '1.txt'; Query OK, 4 rows affected (0.02 sec)

这个方法很值得继续研究学习 !!

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。