1. 程式人生 > >Mysql(三)-數據導入,導出、管理表記錄、條件匹配、表查詢方式

Mysql(三)-數據導入,導出、管理表記錄、條件匹配、表查詢方式

數據導入導出、管理表記錄、條件匹配

##############################################################################################

一、數據導入:把系統文件內容存儲到數據庫的表裏

命令:
load data infile "目錄/文件" into table 表名 fields terminated by "字段分隔符" lines terminated by "行分隔符";

舉例:將/etc/passwd 導入到 studb.user

create databases studb; //創建studb庫
use studb; //使用庫
create table user( //創建表
name varchar(50),
password char(1),
uid int(2) zerofill,
gid int(2) zerofill,
comment varchar(50),
index(name)
);

desc studb.user; //查看表結構


alter table studb.user add id int(2) primary key auto_increment first;

導入:
查看數據庫使用的默認目錄:
show variables like "secure_file_priv"
設置數據庫使用的默認目錄:
mkdir /mysqlload
chown mysql /mysqlload
vim /etc/my.cnf
[mysql]
secure_file_priv="/mysqlload"

cp /etc/passwd /mysqlload/ //將要 導入的文件放到數據庫默認目錄下
load data infile "/mysqlload/passwd" into table user fields terminated by ":" lines terminated by "\n";
//passwd文件的字段之間的分隔符是 : 行間分隔符是回車(\n)

查看結果:
select * from studb.user;

############################################################################################

二、數據導出:把表記錄存儲到系統文件中

sql查詢 into outfile "/目錄/文件名"
sql查詢 into outfile "/目錄/文件名" fields terminated by "符號" lines terminated by "符號";

select * from bbs.t1 into outfile "/mysqlload/a.txt";

//註意:因為之前我已經設置了數據庫使用的默認目錄,所以我的文件導出路徑就是我設置的目錄下(/mysqlload)。

#########################################################################################################

三、管理表記錄:
1、插入表記錄
insert into 庫.表 values (字段值列表);
insert into 庫.表 values (字段值列表),(字段值列表);

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

2、查詢表記錄:
select 字段名列表 from 庫.表;
select 字段名列表 from 庫.表 where 條件;

select * from user;
select * from user where name="mysql";

3、更新表記錄:
update 庫.表 set 字段1=值,字段2=值,字段n=值;
update 庫.表 set 字段1=值,字段2=值,字段n=值 where 條件表達式; //修改滿足條件的字段的值

4、刪除表記錄:
delete from 庫.表 where 條件表達式; //刪除滿足條件的字段的表記錄
delete from 庫.表; //刪除所有表記錄



################################################################################################################

//註意:之前已經將/etc/passwd 導入到studb.user 中,所以接下來操作以user表為例:


四、條件匹配的表達方式:
數值比較: < <= > >= = !=
字符比較: = !=
範圍匹配:between 值1 and 值2 ; in ; not in
匹配空和非空:is null ; is not null;
邏輯匹配:and ; or
模糊查詢: where 字段名 like '表達式';
正則匹配:where 字段名 regexp '正則表達式';

1、數值比較:字段類型必須為數值:
數值比較 > >= < <= = !=
字段名 符號 值
select name from user where uid=15;
select * from user where id>10;

###########################################################

2、字符比較 = !=
字段名 符號 "值"
select name,shell from user where shell!="/bin/bash";
select id,name from user where name="apache";

###########################################################

3、範圍內匹配
字段名 between 值1 and 值2 在...之間

select * from user where id between 10 and 15;
select name from user where uid between 1 and 10;

字段名 in (值列表) 在...裏
select id,name from user where name in ("apache","root","bob");
select id,name,uid from user where uid in (10,15,9,12);

字段名 not in (值列表) 不在...裏
select name from user where uid not in (0,1,5,7);
select * from user where name not in ("root","mysql","bin");

#####################################################################

4、匹配空 is null
字段名 is null
匹配非空 is not null
字段名 is not null
select id from user where name is null;
select id,name,shell from user where shell is not null;


insert into user(name)values(""),("null"),(null);
select id,name from user where name="";
select id,name from user where name="null";

############################################################################

5、distinct 不顯示重復值
distinct 字段名
select distinct shell from user;
select distinct shell from user where uid<=10;

#########################################################################

6、邏輯匹配 : 有多個條件
邏輯與 and 多個條件必須都成立
邏輯或 or 多個條件有一個條件成立即可
邏輯非 ! 取反


select name from user where name="zhangsan" and

uid=500 and shell="/bin/bash";

select name from user where name="zhangsan" or uid=500

or shell="/bin/bash";

###########################################################################


7、數學運算操作 + - * / %
字段類型必須是數值類型

select 字段名 符號 字段名 from 表 where 條件;

select uid+gid from user where name="root";
select name,uid,gid,uid+gid he from user;
select name,uid,gid,uid+gid he from user where name="bin";

alter table user add age tinyint(2) unsigned default 21 after name;

select name,age,2017-age old from user where name="bob";

select name,uid,gid,(uid+gid)/2 pjz from user where name="bin";

###############################################################################
8、模糊查詢 like
where 字段名 like '表達式';
_ 任意一個字符
% 0個或多個字符
select name from user where name like '_ _ _ _';
//匹配name字段,值為4個字符的值。
select name,uid from user where name like '_ _ _ _' and uid<=10;
select name from user where name like 'a%';
//匹配a開頭的
select name from user where name like '%a%';
//匹配字段值中含有a的

select id,name from user where name like '_a_';
//匹配字段值為3個字符而且中間一個字符為a的
select id,name from user where name like 'j%' or "%y";
//匹配j開頭,或者y結尾的

###############################################################################
9、正則匹配
where 字段名 regexp '正則表達式';
. 任意單個字符
^ 以什麽開頭
$ 以什麽結尾
[ ] 範圍內匹配
* 前面字符出現0到多次
| 或

insert into user(name) values("bob9"),("j7im"),("1yaya");

select name from user where name regexp '[0-9]';
//字段值中含有數字的
select name from user where name regexp '^[0-9]';
//字段值的以數字開頭的
select name,uid from user where uid regexp '..';
//字段值,字符數最少為2個的字段值
select name,uid from user where uid regexp '^..$';
//字段值,字符數為2個的字段值

select name,uid from user where name regexp 'a.*t';
//字段值中有a且有t
select name,uid from user where name regexp '^a.*t';
//以a開頭且有t
select name,uid from user where name regexp '^r|t$'
//以r開頭或者t結尾

################################################################

10、統計函數 字段得是數值類型。
求和 求平均值 求最大值 最小值 統計個數
sum(字段名) avg(字段名) max(字段名) min(字段名) count(字段名)

select count(name) from user where shell="/bin/bash";
select max(uid) from user;
select min(gid) from user;
select avg(age) from user;
select sum(gid) from user;
select sum(gid) , count(name) from user;

########################################################################
11、查詢排序 sql查詢 order by 字段名 asc/desc;
select name,uid from user where uid between 10 and 50 ;
//顯示name,uid字段,並且滿足uid在10到50之間,默認升序排列
select name,uid from user where uid between 10 and 50 order by uid desc; //desc降序排列

##########################################################################

12、查詢分組 sql查詢 group by 字段名;
select shell user where uid between 10 and 50 ;
select shell from user where uid between 10 and 50 group by shell;
select shell from user group by shell;

//因為不同用戶的shell可能相同,所以將相同的shell進行分組,作用與distinct(不顯示重復值類似)。

################################################################################

13、限制查詢顯示行數 limit
sql查詢 limit 數字; 顯示查詢結果的前幾行
sql查詢 limit 數字1 , 數字2; 從數字1的下一行開始顯示,數字2設置總共顯示多少行
select * from user;
select * from user limit 2 ;
//顯示前兩行
select * from user limit 2 ,2 ;
//從第3行開始顯示,總共顯示2行,所以就顯示的時第3,4行
select * from user order by uid desc;
select * from user order by uid desc limit 5;
select * from user order by uid desc limit 1;

###############################################################################################
四:表查詢方式
單表查詢
where嵌套查詢
多表查詢
連接查詢


1、單表查詢
前面所用的都屬於單表查詢。

#############################################################


2、where嵌套查詢 :把內層的查詢結果作為外層查詢的查詢條件。

select 字段名列表 from 表名 where 條件 ( select 字段名列表 from 表名 where 條件 );


select name,uid from user where uid > ( select avg(uid) from user );

//顯示用戶名和uid uid字段的值 大於 uid字段的平均值。(同一張表)

select name from user where name not in (select user from mysql.user );

//顯示用戶名,studb.user 中的哪些用戶名不在mysql.user表中(不同表)

select name from user where name in (select user from mysql.user where user="zhangsan");

select name from user where name not in (select user from mysql.user where user="zhangsan";);


########################################################################################

復制表: 作用: 快速建表 、 備份表

create table 庫.表 sql查詢;

create database dbbak;
create table dbbak.user2 select * from studb.user;
create table dbbak.user3 select * from studb.user where 1 = 2;
create table dbbak.user4 select name,uid from studb.user limit

3:多表查詢
select 字段名列表 from 表名列表; 迪卡爾集
select 字段名列表 from 表名列表 where 條件;

create table studb.t1 select name,uid,shell from user limit 3;
create table studb.t2 select name,uid,homedir from user limit 4;
show tables;

select * from t1; select * from t2;
//總共3*4=12條表記錄

select * from t1,t2 where t1.uid = t2.uid and t1.name=t2.name;

//查詢t1和t2中uid與name相同的字段,並顯示
select t1.* , t2.homedir from t1,t2 where t1.uid = t2.uid and t1.name=t2.name;
//顯示t1中所有字段,t2中的homedir字段,滿足t1.uid = t2.uid and t1.name=t2.name;

++++++++++++++++++++++
4、連接查詢
左連接查詢
select 字段名列表 from 表A left join 表B on 條件;

右連接查詢
select 字段名列表 from 表A right join 表B on 條件;

create table studb.t3 select name,uid,shell from user limit 3;
create table studb.t4 select name,uid,shell from user limit 5;
show tables;
select * from t3; select * from t4;

select * from t3 left join t4 on t3.uid=t4.uid;
//以t3為參照表,顯示結果
+--------+------+---------------+--------+------+---------------+
| name | uid | shell | name | uid | shell |
+--------+------+---------------+--------+------+---------------+
| root | 0 | /bin/bash | root | 0 | /bin/bash |
| bin | 1 | /sbin/nologin | bin | 1 | /sbin/nologin |
| daemon | 2 | /sbin/nologin | daemon | 2 | /sbin/nologin |
+--------+------+---------------+--------+------+---------------+


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

//以t4為參照表,顯示結果:
+--------+------+---------------+--------+------+---------------+
| name | uid | shell | name | uid | shell |
+--------+------+---------------+--------+------+---------------+
| root | 0 | /bin/bash | root | 0 | /bin/bash |
| bin | 1 | /sbin/nologin | bin | 1 | /sbin/nologin |
| daemon | 2 | /sbin/nologin | daemon | 2 | /sbin/nologin |
| NULL | NULL | NULL | adm | 3 | /sbin/nologin |
| NULL | NULL | NULL | lp | 4 | /sbin/nologin |
+--------+------+---------------+--------+------+---------------+

#################################################################################

Mysql(三)-數據導入,導出、管理表記錄、條件匹配、表查詢方式