本文共 5297 字,大约阅读时间需要 17 分钟。
##########################################
数据导入与导出(1)数据导入mysql>load data infile "目录/文件名" into table 表名 /默认目录为 /var/lib/mysql-files,可修改配置文件改变默认目录,如不修改配置文件,默认且一定要在这个目录下 >field terminated by "字段间隔符" /导入的文本数据的字段间隔符号 >lines terminated by "\n" /导入的文本数据的行结束符号mysql> show variables like "secure_file_priv"; 查看默认目录/etc/my.cnfsecure_file_priv="/myfile" 把/etc/passwd 数据导入到表user中 a.创建表user(表中字段要与导入的数据中字段相同) create table user( >name char(15), >passwd char(8), >uid int, >gid int, >comment varchar(50), >homedir varchar(30), >shell varchar(30), >index(name), >unique index(uid) >); b.把/etc/passwd 数据放入默认目录/var/lib/mysql-files cp /etc/passwd /var/lib/mysql-files/ c.数据导入 load data infile "/var/lib/mysql-files/passwd" into table user \ >fields terminated by ":" lines terminated by "\n"; /此表中字段间隔符为":",行结束符号为"\n". d.验证 select * from user; (2)数据导出 把表记录通过sql查询存储到系统文件中 >sql查询 into outfile "目录/文件名" fields terminated by "字段间隔符" lines terminated by "行结束符号" /目录默认/var/lib/mysql-files 如不修改配置文件,默认且一定要在这个目录下 #把uid小于100的数据导出到user1.txt文件中 >select * from user where uid < 100 into outfile "/var/lib/mysql-files/user.txt" \ >fields terminated by "*"\ /字段间隔符为 * >lines terminated by "#" /行间隔符为 ##################################################################################管理表记录增insert into 库名.表名 values(值列表),(值列表); /给所有字段赋值insert into 库名.表名(字段名列表) values(值列表),(值列表); /只给个别字段赋值查select * from 库名.表名; /查询表所有字段数据select 字段名列表 from 库名.表名 /查询个别字段数据#select name ,uid from user;select * from 库名.表名 where 条件;#select * from user where name="root"; /查询name字段为root的所有数据条件匹配的表示方式: 数值比较 > >= < <= = !=字符比较 = !=范围内匹配(a) 字段名 in (值列表) 在...里#select * from user where name in ("root","tom","apache"); /字段名匹配几个 查询几条(b)字段名 between 值1 and 值2 在...之间#select * from user where uid between 10 and 50; /字段名匹配几个 查询几条(c)not in#select * from user where name not in ("root","tom","apache"); /相当与 in 的取反,匹配的都不显示匹配空/非空(a) is null#select * from user where name is null;(b) is not null#select * fro user where name is not null;不显示重复值(a)distinct 字段名#select shell from user; /查询表中的shell字段数据 ,其中有很多重复值#select distinct shell from user /把重复的值只显示一次,可以快速查看此字段中都有那些数据逻辑匹配 : 有多个条件逻辑与 and 多个条件必须都成立逻辑或 or 多个条件有一个条件成立即可逻辑非 ! 取反#select name from user where name="tom" and uid=500; /查询 name字段为tom 且uid=500 ,显示name字段数据 条件都必须成立#select name from user where name="root" or uid=100; /查询name字段数据 其条件为 name=“root” 或者uid=100数学运算操作 + - * / % 字段类型必须是数值类型#select uid,gid,uid+gid he from user; /查询 uid,gid 和uid+gid 和 的字段信息 he(uid+gid的值的字段名,可变)模糊查询where 字段名 like '表达式'(a)"_" 表示一个字符#select name from user where name like 'r_o_t'; /一个'_'表示一个字符(b) % 0个或多个字符#select name from user where name like 'r%'; /查询name字段r开头的所有数据#select name from user where name like '%r%'; /查询name字段包含r的数据#select name from user where name like '%t'; /查询name字段t结尾的所有数据#select name from user where name liek 'r%' or name like '%t'; /以r开头或者t结尾正则匹配where 字段名 regexp '正则表达式'#select name from user where name regexp '[0-9]'; /查询name字段中有数字的数据#select name from user where name regexp '^[0-9]'; /查询name字段中 数字开头的数据#select name from user where name regexp '^r'; /查询name字段中 r开头的数据#select name from user where name regexp 'r.*t'; /查询name字段中,r t之间包含0个或多个字符的数据统计函数 字段得是数值类型(a)sum(字段名) 求和#select sum(uid) from user;(b)avg(字段名) 平均值#select avg(uid) from user;(c)max(字段名) 最大值#select max(uid) from user;(d)min(字段名) 最小值#select min(uid) from user;(e)count(字段名) 统计个数#select sum(uid) from user; /不统计字段值为null 查询排序>sql查询 order by 字段名 /默认升序 desc 降序#select uid from user order by uid; /查询uid字段数据并升序排序#select uid from user order by uid desc; /查询uid字段数据并降序排序#select * from user order by uid; /查询表中所有数据并以uid升序排序查询分组>sql查询 group by 字段名 /把相同的数据放在group组里,相当于不显示重复值#select shell from user group by shell; 限制查询显示行数 (a)limit 数字n; 显示前n行#select * from user limit 3;#select * from user where uid between 10 and 50 limit 3;(b)limit 数字m,数字n; 显示m行后的n行#select * from user limit 1,2; 显示第1行后的两行内容 相当于显示2-3行内容#select * from user limit 1,1; 显示第二行内容嵌套查询 把内层的查询结果作为外层的查询条件select * from user where 条件 (select * from 表名 where 条件);#select * from user where name in (select name from user where uid<10);#select name,uid from user where uid > (select avg(uid) from user);复制表 作用:快速建表 、 备份表create table 表名 sql查询#create table user1 select name,uid,homedir from user limit 3; /把sql查询的结果作为新表的结构及数据,#create table user2 select name,uid,shell from user limit 4;多表查询select 字段名列表 from 表名列表;笛卡尔集#select * from user1,user2; /显示为 3*4 12 行,user1表的每一行对应user2表的每一行#select * from user1,user2 where user1.name=user2.name and user1.uid=user2.uid; /显示 user1 与user2 name,uid字段相等的行连接查询(a)左连接查询 以左边表(A)为主查询某个条件下 表A,表B的数据select * from 表A left join 表B on 条件;#select * from user1 left join user2 on user1.uid=user2.uid;(b)右连接查询 以左边表(B)为主查询某个条件下 表A,表B的数据#select * from user1 right join user2 on user1.uid=user2.uid;改修改某字段内的记录(a)update 表名 set 字段名="";#update user1 set uid=3;(b)update 表面 set 字段名="" where 条件;#update user2 set uid=2 where name="root";删删除记录(a)delete from 表名; /删除表中记录#delete from user1;(b)delete from 表名 where 条件; /删除某个条件下的一行记录#delete from user1 where name="root";
转载于:https://blog.51cto.com/13402236/2056794