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

相關文章
相關標籤/搜索