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   條件;//條目多的放在右邊
























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章