mysql系列之3----数据导入导出,管理表,查询

一、数据导入与导出

1、搜索系统的目录:show variables like "secure_file_priv"

 //如果显示为空的话,可以去配置文件里面设置路径,并拷贝文件到允许的目录下,设置权限

  +------------------+-----------------------+

     | Variable_name    | Value                 |

     +------------------+-----------------------+

    | secure_file_priv | /var/lib/mysql-files/ |

        +------------------+-----------------------+

 可以看到其安全的目录为:/var/lib/mysql-files

2、复制表到安全目录下:

  cp /etc/passwd      /var/lib/mysql-file/

3、导入表:首先创建相应的数据库和表,然后再导入

   load data infile "/var/lib/mysql-files/passwd"  //导入表文件的路径

   into table test.user                    //导入哪个数据库下的哪个表

   fields terminated by ":" lines terminated by "\n";   //分隔符和每行的结尾符

4、数据的导出:

   select * from test.user limit 3 into outfile "/var/lib/mysql-files/user3.txt" //前三行导出

   fields terminated by "*" lines terminated by "\n";   //指定字段分隔符

二、管理表记录

1、查询表记录:select 字段名列表 from 库.表 where 匹配条件

2、匹配条件的表示方式:

A、数值比较 = != > < 等

B、字符比较   =     !=

C、范围内比较:where  字段名  between  值1   and  值2;在。。。。。。之间

                                                              in  (值列表) ;在。。。。。里

                                                              not in (值列表)   ;不在..............里

D、逻辑匹配:and    or   !

E、匹配空,非空 : is null;    is not null;          distinct //重复值不显示,加在select后面

F、运算操作:select   name ,2018-s_year as age  from name ="root";

G、模糊查询:where  字段名   like '表达式'   :  %   //0个或多个字符         _    //一个字符

H、正则匹配:where     字段名   regexp    '正则表达式'    :   '^....$' 四个数字

I、统计函数:求和    sum(字段),    平均值  avg(字段)

    最大值  max(字段),     最小值  min(字段),         统计个数  count(字段)

   select sum(user_id) from sys_in;      distinct :不显示字段的重复值

3、查询结果分组:

 select * from stuin order by age;    //默认升序排列

   select * from stuin order by age desc;   //降序排列

 select sex,count(sex) from stuin group by sex; //统计性别总数以sex排序

SELECT sex AS '性别',count(sex) AS '人数' FROM stuin GROUP BY sex;

4、更新表记录字段的值

   update      表名    set   字段=值   where   条件;

5、删除表记录:

 delete    from   表名    where  条件;

6、嵌套查询

  select  user,uid  from  user where uid>(select avg(uid) from user where uid<500);

  //查询uid>(uid<500的帐号的平均值)的记录

7、复制表:key属性不会复制给新表

create table 表2 select * from 表1 where 条件 ;

8、多表查询:不加条件(笛卡尔集)

select  字段  from   表1,表2    where   条件;

9、左右连接

select  字段名列表  from  表1  left   join  表2   on  条件;//条目少的放在左边

select  字段名列表  from  表1  right  join  表2  on   条件;//条目多的放在右边

猜你喜欢

转载自blog.51cto.com/14421478/2414999