MySQL学习笔记(5)
时间:2006-12-12 来源:bitterness
mysql操作语言
----------------------------
mysql select语法
select 列1,列2,... from 表1,表2,...
[where 条件]
[group by ...]
[having ...]
[order by ...] 排序
select * from tablename;
select count(*) from tablename;
select xx, left(xx,4) from tablename;
//某个字段前几位
where xx >= "xx"
and .. and ..
xx < "xx";
where xx like "%xx%"
%包含任意字符串
_包含任意一个字符
max()
嵌套:
select * from emp
where sal in
(select max(sal) from emp group by deptno);
--
order by xx desc //这样就从高到底排序
limit 0,30 每页30行,从0行开始
limit子句可以被用于强制select语句返回指定的记录数,
参数必须是一个整数常量,如果给定二个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目n,初始偏移量为0(而不是1)
select * from emp limit 3,5;
查看第3-7的信息
select * from emp order by sal limit 0,5;
(=select * from emp order by sal limit 5;)
查看薪水最低的前5位员工信息
-----
员工表emp(empno,name,tel,deptno,sal)
部门表dept(deptno,dname,addr)
显示职工的职工号,姓名,部门名称
select empno,name,dname from emp e,dept d
where e.deptno=d.deptno;
select empno,name,dname from emp join dept using (deptno);
select empno,name,dname from emp e join dept d on e.deptno=d.deptno;
-------------
mysql insert语法
insert into 表(列1,列2,...) values(值1,值2,...)
insert into 表 values(值1,值2,...)
--------------
mysql update语法
update 表 set 列1=值,列2=值2,......[where 条件]
--------------
mysql delete语法
delete从表中删除where子句中给定条件的记录行,并返回删除的记录数目
delete from 表 [where条件];
delete from tabel; //清空table
--------------
mysql truncate语法
清除表全部数据,速度比delete快
truncate table 表
相当于
|delete from 表;
|coummit;
删减操作并重建表,这将比一个接一个地delete记录行要快得多
---------------
mysql load date语法
加载数据,
load data infile 文件 into table 表 [fields terminated by '字符'];
vi teacher.txt
++++++++++++++
各个字段之间用tab分隔,或用,分隔
+++++++++++++++++++++++++++++++++++++++++++
3 张三 武汉大学 1956-01-21
4 李四 武汉大学 1972-05-06
+++++++++++++++++++++++++++++++++++++++++++++
mysql school
mysql>load data infle '/tmp/teacher.txt' into table teacher;
或
mysqllimport school /tmp/teacher.txt
mysql>load data infle '/tmp/teacher.txt' into table teacher fields terminated by ',';
terminated by 指明是用,还是TAB 分隔
---
desc teacher;
加到stu表在做txt文件名最好用stu.txt
select * from teacher;
select * from teacher into outfile '/tmp/test.txt';
导出数据到test.txt中
----------------------------
mysql select语法
select 列1,列2,... from 表1,表2,...
[where 条件]
[group by ...]
[having ...]
[order by ...] 排序
select * from tablename;
select count(*) from tablename;
select xx, left(xx,4) from tablename;
//某个字段前几位
where xx >= "xx"
and .. and ..
xx < "xx";
where xx like "%xx%"
%包含任意字符串
_包含任意一个字符
max()
嵌套:
select * from emp
where sal in
(select max(sal) from emp group by deptno);
--
order by xx desc //这样就从高到底排序
limit 0,30 每页30行,从0行开始
limit子句可以被用于强制select语句返回指定的记录数,
参数必须是一个整数常量,如果给定二个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目n,初始偏移量为0(而不是1)
select * from emp limit 3,5;
查看第3-7的信息
select * from emp order by sal limit 0,5;
(=select * from emp order by sal limit 5;)
查看薪水最低的前5位员工信息
-----
员工表emp(empno,name,tel,deptno,sal)
部门表dept(deptno,dname,addr)
显示职工的职工号,姓名,部门名称
select empno,name,dname from emp e,dept d
where e.deptno=d.deptno;
select empno,name,dname from emp join dept using (deptno);
select empno,name,dname from emp e join dept d on e.deptno=d.deptno;
-------------
mysql insert语法
insert into 表(列1,列2,...) values(值1,值2,...)
insert into 表 values(值1,值2,...)
--------------
mysql update语法
update 表 set 列1=值,列2=值2,......[where 条件]
--------------
mysql delete语法
delete从表中删除where子句中给定条件的记录行,并返回删除的记录数目
delete from 表 [where条件];
delete from tabel; //清空table
--------------
mysql truncate语法
清除表全部数据,速度比delete快
truncate table 表
相当于
|delete from 表;
|coummit;
删减操作并重建表,这将比一个接一个地delete记录行要快得多
---------------
mysql load date语法
加载数据,
load data infile 文件 into table 表 [fields terminated by '字符'];
vi teacher.txt
++++++++++++++
各个字段之间用tab分隔,或用,分隔
+++++++++++++++++++++++++++++++++++++++++++
3 张三 武汉大学 1956-01-21
4 李四 武汉大学 1972-05-06
+++++++++++++++++++++++++++++++++++++++++++++
mysql school
mysql>load data infle '/tmp/teacher.txt' into table teacher;
或
mysqllimport school /tmp/teacher.txt
mysql>load data infle '/tmp/teacher.txt' into table teacher fields terminated by ',';
terminated by 指明是用,还是TAB 分隔
---
desc teacher;
加到stu表在做txt文件名最好用stu.txt
select * from teacher;
select * from teacher into outfile '/tmp/test.txt';
导出数据到test.txt中
相关阅读 更多 +