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

1、数据导入与导出
mysql

  一、搜索系统的目录:show variables like "secure_file_priv"   正则表达式

     //若是显示为空的话,能够去配置文件里面设置路径,并拷贝文件到容许的目录下,设置权限sql

      +------------------+-----------------------+数据库

         | Variable_name    | Value                 |安全

         +------------------+-----------------------+ide

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

            +------------------+-----------------------+ui

     能够看到其安全的目录为:/var/lib/mysql-filesspa

  二、复制表到安全目录下:regexp

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

  三、导入表:首先建立相应的数据库和表,而后再导入

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

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

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

  四、数据的导出:

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

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


2、管理表记录

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

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

   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 :不显示字段的重复值

  三、查询结果分组:

     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;

 四、更新表记录字段的值

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

 五、删除表记录:

     delete    from   表名    where  条件;

 六、嵌套查询

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

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

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

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

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

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

 九、左右链接

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

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

相关文章
相关标签/搜索