mysql series within 3 ---- importing and exporting data, management tables, queries

A data import and export

1, a directory search system: show variables like "secure_file_priv"

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

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

     | Variable_name    | Value                 |

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

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

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

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

2. Copy the table to a safe directory:

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

3, import the table: first create the appropriate databases and tables, and then import

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

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

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

4, data export:

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

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

Second, the management table records

1, record lookup table: select list of field names from the database table where the matching conditions.

2, the matching condition expressed by:

A, = numerical comparison! => <Etc.

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, grouping query results:

 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, updates the value table record fields

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

5, delete the table records:

 delete    from   表名    where  条件;

6, nested query

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

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

7, copy table: key attributes are not copied to the new table

create table TABLE 2 select * from Table 1 where condition;

8, multi-table query: without conditions (Descartes set)

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

9, left and right connections

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

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

Guess you like

Origin blog.51cto.com/14421478/2414999