码迷,mamicode.com
首页 > 数据库 > 详细

初识Mysql(三)

时间:2018-01-21 19:22:07      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:指定字段   family   UI   and   min   truncate   mkdir   page   rgb   

1 数据导入

1.1 查看默认使用的目录及目录是否存在:

mysql> show variables like "secure_file_priv";

数据导入导出使用的目录:/var/lib/mysql-files/

1.2 修改目录

vim /etc/my.cnf  #修改配置文件中导入数据存放的路径

[mysqld]

secure_file_priv=”/myload”

mkdir /myload ;chown mysql /myload #创建目录及修改所有者(mysql写)

systemctl restart mysqld  #重启服务

注意selinux需要关闭

mysql> show variables like "secure_file_priv"; #此时路径已改变

1.3 导入格式

load data infile “目录名/文件名” into table 表名

fields terminated by “分隔符”

lines terminated by “\n”;

2 数据导出

2.1 导出格式

select 字段名(*) from 表名 into outfile “/目录/文件” ;

fields terminated by “分隔符”lines terminated by “\n”;   #可加可不加

3 管理表记录

3.1 增 插入表记录

 一次插入1条记录给所有字段赋值

insert into 表名 values(值列表);

 一次插入多条记录给所有字段赋值

insert into 表名 values(值列表),(值列表),(值列表);

 一次插入1条记录给指定字段赋值

insert into 表名(字段名)  values(值列表);

 一次插入多条记录给指定字段赋值

insert into 表名(字段名)  values(值列表),(值列表),(值列表);

 

3.2 查询表记录

3.2.1单表查询

select 字段名 from 表名;

select 字段名 from 表名 where 条件;


3.2.2where嵌套查询

把内层查询结果作为外查询的条件

同表查询:

select 字段名 from 表1 where 条件 (select 字段名 from 表1);

select 字段名 from 表1 where 条件(select 字段名 from 表1 where条件);

不同表查询:

select 字段名 from 表1 where 条件 (select 字段名 from 表2);

select 字段名 from 表1 where 条件 (select 字段名 from 表2 where条件);

3.2.3多表查询:

格式1:select 字段 from 表1,表2; (笛卡尔集,多匹配少,出现重复)

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

连接查询:

内连接查询:select 字段 from 表1,表2;

外连接查询:

左连接查询(以左表记录为主)

select 字段 from 表1 left join 表2 on 条件;

右连接查询(以右表记录为主)

select 字段 from 表1 right join 表2 on 条件;

案例:新建两张表,分别取/etc/passwd的前3和前5行

select * from t3,t4;

select * from t3,t4 where t3.name=t4.name;

select * from t3 left  join t4 on t3.uid=t4.uid ;

select * from t3 right join t4 on t3.uid=t4.uid ;

select t3.* from t3 left  join t4 on t3.uid=t4.uid ;

select t3.shell from t3 left  join t4 on t3.uid=t4.uid ;

 

3.3 匹配条件的表示方式

3.3.1 数值比较

= ; <>或!= ; > ; < ; >= ; <= ; between ...and ; like ;  

where 字段名 between 值1  and 值2;  #在...之间

3.3.2 逻辑比较

or ; and ; ! (多个查询条件)

in ; not in ;

where 字段名 in(值列表);        #在...内

where 字段名 not in(值列表);    #不在...内

is null ; is not null;

select * from userinfo where user=””;

select * from userinfo where user=”null”;

select * from userinfo where 字段 is null;

distinct

select distinct 字段 from 表名;  #从字段中筛选出不同的值

3.3.3 运算操作

+ - * / %

mysql> select user,2018-s_year as age from userinfo

where user="root";

mysql> select uid,gid,uid+gid as sum from userinfo

where user="mysql";

mysql> select uid,gid,uid+gid as sum,(uid+gid)/2 as avg 

from userinfo where user="mysql";

3.3.4 模糊查询 like

where 字段名 like “通配字串”;

_ : 匹配单个字符

% : 匹配0个或多个字符

3.3.5使用正则表达式

where 字段名 regexp ‘正则表达式’;

3.4 常用函数

avg() : 集合的平均值

sum() : 对集合的各参数求值

min() : 集合中的最小值

max() : 集合的最大值

count() : 记录的个数 #空值不计算在内

3.5 查询结果排序/分组

order by 字段名  [asc|desc];

select查询 order by 字段名  asc/desc;

group by 字段名  [having 条件表达式]

select查询 group by 字段名

和select distinct查询一样(查找不重复的)

limit  n,m  #限制显示记录条目数

n 表示起始行 第一行的编号是0     m 表示总行数

mysql> select *from userinfo limit 1,1;  #显示第二行(1行以后的1行)

3.6 更新记录

update 表名 set 字段=值,字段=值;    #批量修改

update 表名 set 字段=值,字段=值  where 条件;  #按条件修改

3.7 删除记录

delete from 表名;#删除所有

delete from 表名 where 条件: #删除符合条件的

truncate table 表名; #快速删除所有数据

3.8 表的复制及改名

将源表xxx复制为新表yyy

create table yyy select * from xxx;

将指定的查询结果复制为新表zzz

create table zzz select查询;

注意:复制过来的新表的key值是无法继承的;

将源表的结构复制到新表vvv

create table vvv select * from xxx where false;(false条件不成立)

将源表改名为mmm

alter table xxx rename to mmm;


初识Mysql(三)

标签:指定字段   family   UI   and   min   truncate   mkdir   page   rgb   

原文地址:http://blog.51cto.com/13452945/2063423

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!