温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

数据导入导出 、 表记录基本操作 、 查询及匹配条件 、 多表查询 、

发布时间:2020-06-23 21:30:48 来源:网络 阅读:955 作者:大米叔叔 栏目:MySQL数据库

数据管理:(管理表记录)
数据导入:把系统文件的内容存储到数据库的表里.
mysql> create table uesr(
-> name varchar(50),
-> mima char(1),
-> UID int(2),
-> GID int(2),
-> ms varchar(100),
-> honame char(100),
-> jsq char(25),
-> index(name)
-> );

查看默认使用目录及目录是否存在: show variables inke "secure_file_priv"

格式: load data infile "系统目录/文件名" into table 库名.表名 fields treminated by "字段间隔符号" lines treminated by "\n" 数据导入
例: load data infile "/var/lib/mysql-files/passwd" into table uesr fields terminated by ":" lines terminated by "\n" 格式 "\n";
系统目录/文件名 表名 指定分隔符 "字段间隔符号" 列指定的分隔符是"\n"
添加一个行号方便后续工作的查找:alter table uesr add id int(2) primary key auto_increment first;
表名 添加 ID 类型
注意事项:
字段分隔符要与文件内的一致
指定导入文件的绝对路径
导入数据的表字段类型要与文件字段匹配
禁用SElinux
数据导出:把表记录存储到系统文件里.
select 查询 into outfile "目录/文件名"; [fields terminated by "符号" lines terminated by "符号"];
select id,name from uesr into outfile "/var/lib/mysql-files/uesr1.txt"

格式:

注意事项:导出的内容是由SQL查询语句

limit 选取第5行
lines terminated by "\n" 行分隔符
fields terminated by "分隔符" 字段分隔符

管理表记录:
增: insert into 库.表 values(字段值列表); #(一次给一个字段赋值)
insert into 库.表 (字段值列表) values (字段值列表); ##(指定字段赋值)
查:
select 字段名列表 from 库.表 wher 条件 #全部查询.并且条件匹配 写wher 条件的话就是匹配表中什么条件里的.
select 字段名列表 (列) from 库.表 where 条件(行) ; //指定查找
条件匹配的表示方式:
数值比较 > >= < <= = !=
字段名 符号 值
例: select name from uesr where uid=15; # 查多个时可用逗号分隔.

字符比较: = !=
字段名 符号 "值"
select name,shell from uesr where shell!="/bin/bash";
select name from uesr where name="apache";
范围内匹配
字段名 in (值列表) 在.....里
select id,name from user where name in("apache","root","bob");
select id,name,uid from uesr where uid in (10,15,9,12);
字段名 between 值1 and 值2 在....之间
select from user where id between 10 and 15;
select
from user where uid between 1 and 10;
字段名 not in (值列表) 不在 .......里
select name from user where uid not in(0,1,5,7)
select from user where name not in("root","mysql","bin")
匹配空 is null
字段名 is null
select id from uesr where name is null;
匹配非空 is not null
字段名 is not null
select id,name.shell from uesr where shell is not null;
select id,name from user where name="";
select id,name from user where name="null";
distinct不显示重赋值
distinct 字段名
select destinct shell from user;
关键字
select distinct shell from user where uid<=10;
逻辑匹配: 有多个条件匹配
逻辑与 && and多个条件必须都成立
逻辑或 || or多个条件有一个条件成立即可.
逻辑非 ! 取反.
条件1 &&条件2 条件n 可以用&&连接(三个条件都有)
select name from uesr where name ="zhangsan"and uid=500 and shell="/bin/bash";
表名 列名 内容 与 列名条件 与 列名条件
select name from uesr where name ="zhangsan" or uid=500 or shell="/bin/bash";
运算操作: + -
/ %
select 字段名 符号 字段名 from 表名 where 条件;
例:
select UID + GID from uesr where name="root";
select UID,GID,UID + GID he from uesr where name="root";
模糊查询: like
where 字段名 like '表达式'; ###一般情况下表达式可以用
任意一个字符
% 0个或者多个字符

select name from uesr where name like '___' and UID<=10;
select name from uesr where name like 'a%'; ##用户名里有a的
select id,name from uesr where name like '%
%'; ##包含任意字符的
select name from uesr where name in ("","null") or name is null; #### 匹配空,或者空的名,
正则匹配
where 字段名 regexp '正则表达式';
. ^ $ [ ]
insert into uesr(name)values("bob9"),("j7im"),("1yaya");
select name from uesr where name regexp '[0-9]$'; 数字结尾的名字
select name from uesr where name regexp '^[0-9]'; 数字在前的名字
select name from uesr where name regexp '..'; 任意字符和数字的
select name,uid from uesr where uid regexp '^..$';
select name,uid from user where name regexp 'a.
t'
select * from uesr where name regexp '^r|t$'; 查出r开头t结尾的
统计函数:
求和, 求平均值, 求最大值, 求最小值 统计个数
sum(字段名) avg(字段名) max(字段名) min(字段名) count(字段名)

select count(name) from uesr where jsq="/bin/bash"; #统计
select max(uid) from uesr; #求最大值
select min(gid) from uesr; #求最小值
select avg(age) from uesr; #平均值
select sum(gid) from uesr; #求和
select sum(uid),count(name) from uesr; ##求和统计
查询排序 order by
sql查询 order by 字段名 desc;(asc/desc) #默认升序

select name,uid from uesr where uid between 10 and 50 order by uid desc; 降序
select name,uid from uesr where uid between 10 and 50; 默认升序

查询分组 group by
sql查询 group by 字段名

select shell from user where uid between 10 and 50; 10到50行不显示重复的组,
select 字段名 from 表名 where 字段名 between 10 and 50 group by 字段名;

查询限制行数 limit
sql查询 limit 数字; 显示查询结果的前几行
select * from uesr limit 1 显示查询结果的第一行

sql查询 limit 数字1 , 数字2 ; 设置显示行的范围

select from user; 显示所有
select
from user limit 2; 显示行
select * from user limit 2,5; 显示行的范围

select * from uesr order by uid desc limit 5; #uid用户最大的前五个显示出来
改:
条件匹配的表示方式:
数值比较 字符比较 范围内匹配 匹配空 匹配非空 逻辑匹配
正则匹配 模糊查询
去掉字段重赋值 数字计算 统计函数 分组 排序 限制行数.
单表查询:select 字段名列表 from 库.表 wher 条件
where嵌套查询:把内存的查询结果作为外层查询的查询条件
格式:select 字段名列表 from 表名 where 条件 ( );

#显示用户名和uid号 uid字段的值要大于uid字段的平均值

select name,uid from uesr where uid > (select avg(uid) from uesr); ##同库同表
select name from uesr where name not in (select user from mysql.user); ###不同库不同表
select name from uesr 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.uesr2 select from dc.uesr; 在dbbak这个库里克隆一个dc库里的表
create database dbbak.uesr3 select
from dc.uesr where 1=2;
create database dbbak.uesr3 select name,uid from dc.uesr limit 3; ##备份dc.uesr表里的前三行
库名.表名 列表名,列表名 库,表名 前三行
多表查询:
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;
select from t1,t2 where t1.uid = t2.uid and t1.name=t2.name;
select t1.
,t2/homedir from t1,t2 where t1.uid
连接查询:
左连接查询 : select 字段名列表 from 表A left join 表B on 条件;
select * from t3 left join t4 on t3.UID=t4.UID;
右连接查询 : select 字段名列表 from 表A right join 表B on 条件;

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI