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

本文涉及的产品
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介:

day03
一数据导入
二数据导出
三管理表记录 *
3.1 插入表记录
3.2 查询表记录 (单表 多表 嵌套 连接)
3.3 条件匹配
3.4 更新表记录字段的值
3.5 删除表记录
++++++++++++++++++++++++++++++++
一数据导入 : 把系统文件的内容存储到数据库的表里。
把系统已有的用户信息存储到studb.user表
/etc/passwd
用户名 密码站位符 uid gid 描述信息 家目录 shell

create database studb;
create table studb.user(
name char(50),
password char(1),
uid int(2),
gid int(2),
comment varchar(100),
homedir char(150),
shell char(50),
index(name)
);
查看导入数据时,搜索系统的目录
show variables like "secure_file_priv";
secure_file_priv | /var/lib/mysql-files/

mysql> load data infile "目录名/文件名" INTO TABLE 库.表
FIELDS TERMINATED BY "列间隔符号"
LINES TERMINATED BY "行间隔符号";

mysql> system cp /etc/passwd /var/lib/mysql-files/

mysql> load data 
INFILE "/var/lib/mysql-files/passwd" INTO TABLE studb.user
FIELDS TERMINATED BY ":"
LINES TERMINATED BY "\n";
mysql>alter table studb.user add id int(2) primary key auto_increment first;

select * from studb.user;

++++++++++++++++++++++++++++++++++
修改导入数据时,搜索文件的目录 ?
#vim /etc/my.cnf
[mysqld]
secure_file_priv="/mydatadir"
......
:wq
#mkdir /mydatadir
#chown mysql /mydatadir
#setenforce 0
#systemctl start mysqld
#mysql -uroot -p123456
mysql> show variables like "secure_file_priv";
+------------------+-------------+
| Variable_name | Value |
+------------------+-------------+
| secure_file_priv | /mydatadir/ |
+------------------+-------------+

++++++++++++++++++++++++++++++++++
二数据导出: 把表记录存储到系统文件里。
mysql> show variables like "secure_file_priv";
MySQL> sql查询 INTO OUTFILE "目录名/文件名";
MySQL> sql查询 INTO OUTFILE "目录名/文件名" FIELDS TERMINATED BY "列间隔符号" LINES TERMINATED BY "行间隔符号";

select * from studb.user into outfile "/mydatadir/user1.txt";
system cat /mydatadir/user1.txt

select name,uid from studb.user into outfile "/mydatadir/user2.txt";

select name,uid from studb.user limit 3 ;

select name,uid from studb.user limit 3 into outfile "/mydatadir/user1.txt" fields terminated by "#";

++++++++++++++++++++++++++++++++++
三管理表记录 * (增 删 改 查)
3.1 增 插入表记录
一次插入1条记录给所有字段赋值
insert into 库.表 values(值列表);

insert into user values (51,"jim","x",2001,2001,"my student","/home/jim","/bin/bash");

一次插入多条记录给所有字段赋值
insert into 库.表 values(值列表),(值列表);

insert into user values (52,"jim","x",2001,2001,"my student","/home/jim","/bin/bash"),(53,"jim","x",2001,2001,"my student","/home/jim","/bin/bash");

一次插入1条记录给指定字段赋值
insert into 库.表(字段名列表) values(值列表);
insert into user(name,uid,gid) values("bob",3001,3001);

一次插入多条记录给指定字段赋值
insert into 库.表(字段名列表) values(值列表),(值列表);

insert into user(name,uid,gid) values("bob",3001,3001),("bob",3002,3001);

insert into user(name,password,uid,gid,comment,homedir,shell) values("plj","x",10000,10000,"teacher","/home/plj","/bin/bash");

+++++++++++++++++++++++++++++++++++++++++++++++
3.2 查询表记录 (单表 多表 嵌套 连接)

单表查询 
select 字段名列表 from 库.表;

select 字段名列表 from 库.表 where 匹配条件;

select * from studb.user;

select id,name,homedir from studb.user;

select id,name,homedir from studb.user where id<=10;

select id,name,homedir from studb.user where name="root";

3.3 匹配条件 的 表示方式?
数值比较 = != > >= < <=
where 字段名 符号 数值

select name from user where uid=500;
select name from user where id<=10;

字符比较 = !=
where 字段名 符号 "字符串"
select name from user where shell="/bin/bash";

select name from user where name="daemon";

select name,shell from user where shell != "/bin/bash";

范围内匹配
where 字段名 between 值1 and 值2; 在...之间
where 字段名 in (值列表); 在....里
where 字段名 not in (值列表); 不在....里

select name,uid from user where uid between 10 and 20;

select name from user where name in ("root","lucy","damon");

select name,uid from user where uid in (100,500,1000,2001);

select name from user where shell not in ("/bin/bash","/sbin/nologin");

select name,shell from user where shell not in ("/bin/bash","/sbin/nologin");

逻辑匹配 (多个查询条件时 使用)
逻辑与 and 条件都成立才可以
逻辑或 or 某一个条件成立就可以
逻辑非 ! 取反

select name from user where uid=10 and shell="/bin/bash" ;

select name,uid,shell from user where uid=0 and shell="/bin/bash" ;

select name from user where name="lucy" or name="bob" or uid=1;

select name,uid from user where name!="lucy";

匹配空 is null
匹配非空 is not null

select id ,name from user where name is null;
select id ,name from user where shell is null;
select id ,name,shell from user where shell is null;
select name ,shell from user where shell is not null;

insert into user(id,name)values(61,""),(62,null),(63,"null");

select id,name from user where name="";
select id,name from user where name is null;
select id,name from user where name="null";

DISTINCT 查询时,字段的重复值不显示,只显示第1次出现的值。

select shell from user;
select distinct shell from user;

select shell from user where uid <=1000;
select distinct shell from user where uid <=1000;

查询时做运算操作 + - / %
alter table user add s_year year default 2000 after name;
select 
from user;

select name , 2018 - s_year as age from user where name="root";

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

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

select name,uid,gid, uid+gid as sum, (uid+gid)/2 as avg from user;

模糊查询 like
where 字段名 like '表达式';
% 表示零个或多个字符
_ 表示 一个字符

select id, name from user where name like '%';
select name from user where name like '%a%';
select name from user where name like '_';
select name from user where name like '
%_';
select name from user where name like '
%__';

正则匹配 
. ^ $ [ ] * 
where 字段名 regexp '正则表达式';

insert into user(name) values("yaya9"),("6yaya"),("ya5ya"),("y7aya");

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 '....';
select name,uid from user where uid regexp '^....$'
select name,uid from user where name regexp 'a.b';
select name,uid from user where name regexp '^r.
t$';

统计函数(对字段的值做统计)
sum(字段)求和
avg(字段)求平均值
max(字段)求最大值
min(字段) 求最小值
count(字段) 统计个数

select avg(uid) from user where sex="girl";
select avg(uid) ,sum(uid) from user;
select min(uid),max(uid) from user;
select count(id) from user;
select count(name) from user;
select count(name),count(id) from user;
select count(name) from user where uid >1000;

查询结果分组
sql查询 group by 字段名;
select shell from user where uid<=100 ;

select shell from user where uid<=100 group by shell;

select distinct shell from user where uid<=100;

查询结果排序
sql查询 order by 字段名 asc/desc;

select uid,name from user where uid >=10 and uid<=100;

select uid,name from user where uid >=10 and uid<=100 order by uid;

select uid,name from user where uid >=10 and uid<=100 order by uid desc;

查询结果限制显示记录行数
sql查询 limit 数字;显示查询结果的前几行;
select id,name,uid,shell from user where id<=10 limit 1;
select id,name,uid,shell from user where id<=10 limit 3;

sql查询 limit 数字1,数字2;显示指定范围内的行
数字1 表示起始行 第1行的编号是 0
数字2 表示总行数

select id,name,uid,shell from user where id<=10;
select id,name,uid,shell from user where id<=10 limit 2,3;
select id,name,uid,shell from user where id<=10 limit 4,5;
select id,name,uid,shell from user where id<=10 limit 1,1;
select id,name,uid,shell from user where id<=10 limit 2;

查询表记录的语法格式:
select 字段名列表 from 库.表 where 匹配条件;

条件匹配:
数值比较 = != > >= < <= 
字符比较 = != 
逻辑比较 and or !
范围内匹配 between...and \ in \ not in 
空 is null
非空 is not null
模糊查询 like '表达式' % _ 
正则匹配 regexp '正则表达式' . ^ $ [] 
数学计算 + - 
/ % ( )
统计函数 sum() avg() max() min() count() 
不显示字段的重复值 distinct 字段名
查询分组 group by 字段名
查询排序 order by 字段名 asc/desc
限制显示查询记录的行数 limit 数字;
limit 数字1,数字2; limit 0,1
limit 2,2
++++++++++++++++++++++++++++++++++++++++++++
3.4 更新表记录字段的值 (条件和查询时的一样)
批量修改:
update 库.表 set 字段名=值,字段名="值";

只修改复合条件记录字段的值:
update 库.表 set 字段名=值,字段名="值" where 条件;

update studb.user set s_year=1995 ;
update studb.user set s_year=1990 where name="root";
update studb.user set uid=uid+5 where uid<10;
update studb.user set uid=null where name="bin";
update studb.user set name="" where uid=5;
++++++++++++++++++++++++++++++
3.5 删除表记录 (条件和查询时的一样)
删除全部表记录 delete from 库.表;

删除指定的记录
delete from 库.表 where 条件;
delete from studb.user where uid is null;
+++++++++++++++++++++++++++++++++++++
where嵌套:把内层查询结果作为外查询的查询条件
select 字段名列表 from 库.表 
where 条件 (select 字段名列表 from 库.表 where 条件);

alter table user add liunxsys float(5,2) default 60 after name;
update user set liunxsys=90 where uid=11;
update user set liunxsys=13 where uid=20;
update user set liunxsys=29 where uid=32;

select avg(liunxsys) from user where uid>=10 and uid<=50;

select name ,liunxsys from user where uid>=10 and uid<=50 and liunxsys < avg(liunxsys) ;

select name ,liunxsys from user where uid>=10 and uid<=50 and liunxsys < (select avg(liunxsys) from user where uid>=10 and uid<=50);

select name ,liunxsys from user where liunxsys < (select avg(liunxsys) from user);

select name from studb.user where name in (select user from mysql.user where host="localhost" and user="root");

stuinfo: name iphone class
addrinfo: name addr 
select name from stuinfo where name in (select name from addrinfo where addr="beijing");
select name from stuinfo where name not in (select name from addrinfo where name="zhsan" and addr="beijing");
++++++++++++++++++++++++++++++++++++++++++++++++
复制表 : 
命令格式 create table 库.表 sql查询;
功能 1 快速创建新表 2 备份表

create table studb.user2 select * from studb.user;

create table studb.user3 select * from studb.user order by uid desc limit 5;

create database db4;
create table db4.user5 select * from studb.user where 1 = 2;
key值没有 要自己添加
++++++++++++++++++++++++++++++++++++++++++++++
对象 : 学生 安娜
唯一标识学号
缴费表 110
班级表 110
就业表 110

多表 查询 -----SQL命令格式
select 字段名列表 from 表名列表; (迪卡尔集)

select 字段名列表 from 表名列表 where 条件;只显示与条件匹配的记录。

create database db4;
create table db4.t1 select name,uid,shell from studb.user limit 3;

create table db4.t2 select name,uid,homedir from studb.user limit 5;

select from db4.t1; select from db4.t2;

use db4;
select * from t1,t2; 3x5=15
select t1.name,t1.uid,t2.homedir from t1,t2; 3x5=15

select t1.name,t1.uid,t2.homedir from t1,t2 where t1.name = t2.name;

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

连接查询
左连接查询 
select 字段名列表 from 表A left join 表B on 条件;

右连接查询
select 字段名列表 from 表A right join 表B on 条件;

create table db4.t3 select name,uid,shell from studb.user limit 3;

create table db4.t4 select name,uid,shell from studb.user limit 5;

select from db4.t3; select from db4.t4;

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

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

mysql> select t3.shell from t3 left join t4 on t3.uid = t4.uid
-> group by shell;
+++++++++++++++++++++++++++++++++++++++++++
视图 存储过程 触发器


     本文转自sweak_h 51CTO博客,原文链接:http://blog.51cto.com/13478354/2064355,如需转载请自行联系原作者





相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
2天前
|
人工智能 自然语言处理 JavaScript
2026年Windows+Ollama本地部署OpenClaw保姆级教程:本地AI Agent+阿里云上快速搭建
2026年OpenClaw凭借本地部署、私有化运行的特性,成为打造个人智能体的核心工具,而Ollama作为轻量级本地大模型管理工具,能让OpenClaw摆脱对云端大模型的依赖,实现**本地推理、数据不泄露、全流程私有化**的智能体验。本文基于Windows 11系统,从硬件环境准备、Ollama安装与模型定制、OpenClaw部署配置、技能扩展到常见问题排查,打造保姆级本地部署教程,同时补充阿里云OpenClaw(Clawdbot)快速部署步骤,兼顾本地私有化需求与云端7×24小时运行需求,文中所有代码命令均可直接复制执行,确保零基础用户也能快速搭建属于自己的本地智能体。
4217 18
|
9天前
|
存储 人工智能 负载均衡
阿里云OpenClaw多Agent实战宝典:从极速部署到AI团队搭建,一个人=一支高效军团
在AI自动化时代,单一Agent的“全能模式”早已无法满足复杂任务需求——记忆臃肿导致响应迟缓、上下文污染引发逻辑冲突、无关信息加载造成Token浪费,这些痛点让OpenClaw的潜力大打折扣。而多Agent架构的出现,彻底改变了这一现状:通过“单Gateway+多分身”模式,让一个Bot在不同场景下切换独立“大脑”,如同组建一支分工明确的AI团队,实现创意、写作、编码、数据分析等任务的高效协同。
3597 27
|
13天前
|
人工智能 自然语言处理 监控
OpenClaw skills重构量化交易逻辑:部署+AI全自动炒股指南(2026终极版)
2026年,AI Agent领域最震撼的突破来自OpenClaw(原Clawdbot)——这个能自主规划、执行任务的智能体,用50美元启动资金创造了48小时滚雪球至2980美元的奇迹,收益率高达5860%。其核心逻辑堪称教科书级:每10分钟扫描Polymarket近千个预测市场,借助Claude API深度推理,交叉验证NOAA天气数据、体育伤病报告、加密货币链上情绪等多维度信息,捕捉8%以上的定价偏差,再通过凯利准则将单仓位严格控制在总资金6%以内,实现低风险高频套利。
7190 62
|
3天前
|
人工智能 JSON JavaScript
手把手教你用 OpenClaw + 飞书,打造专属 AI 机器人
手把手教你用 OpenClaw(v2026.2.22-2)+ 飞书,10分钟零代码搭建专属AI机器人!内置飞书插件,无需额外安装;支持Claude等主流模型,命令行一键配置。告别复杂开发,像聊同事一样自然对话。
1562 5
手把手教你用 OpenClaw + 飞书,打造专属 AI 机器人
|
3天前
|
人工智能 运维 安全
OpenClaw极速部署:ZeroNews 远程管理OpenClaw Gateway Dashboard指南+常见错误解决
OpenClaw作为高性能AI智能体网关平台,其Gateway Dashboard是管理模型调用、渠道集成、技能插件的核心操作界面,但默认仅支持本地局域网访问。官方推荐的Tailscale、VPN等远程访问方案在国内网络环境中体验不佳,而ZeroNews凭借轻量化部署、专属域名映射、多重安全防护的特性,成为适配国内网络的最优远程管理解决方案。
1248 2
|
4天前
|
存储 人工智能 BI
2026年OpenClaw(Clawdbot)极简部署:接入小红书全自动运营,一个人=一支团队
2026年的小红书运营赛道,AI自动化工具已成为核心竞争力。OpenClaw(原Clawdbot)凭借“Skill插件化集成、全流程自动化、跨平台联动”的核心优势,彻底颠覆传统运营模式——从热点追踪、文案创作、封面设计到自动发布、账号互动,仅需一句自然语言指令,即可实现全链路闭环。而阿里云作为OpenClaw官方推荐的云端部署载体,2026年推出专属秒级部署方案,预装全套运行环境与小红书运营插件,让零基础用户也能10分钟完成部署,轻松拥有7×24小时在线的“专属运营团队”。
1314 6
|
8天前
|
人工智能 自然语言处理 安全
2026年OpenClaw Skills安装指南:Top20必装清单+阿里云上部署实操(附代码命令)
OpenClaw(原Clawdbot)的强大之处,不仅在于其开源免费的AI执行引擎核心,更在于其庞大的Skills生态——截至2026年2月,官方技能市场ClawHub已收录1700+各类技能插件,覆盖办公自动化、智能交互、生活服务等全场景。但对新手而言,面对海量技能往往无从下手,盲目安装不仅导致功能冗余,还可能引发权限冲突与安全风险。
1978 9
|
1月前
|
人工智能 自然语言处理 Shell
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
本教程指导用户在开源AI助手Clawdbot中集成阿里云百炼API,涵盖安装Clawdbot、获取百炼API Key、配置环境变量与模型参数、验证调用等完整流程,支持Qwen3-max thinking (Qwen3-Max-2026-01-23)/Qwen - Plus等主流模型,助力本地化智能自动化。
46282 159
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
|
1天前
|
人工智能 自然语言处理 安全
OpenClaw双模式部署指南:Windows+Ollama本地私有化+阿里云OpenClaw云端搭建(保姆级教程)
在AI智能体爆发的2026年,OpenClaw凭借本地部署、私有化运行、多工具集成的核心优势,成为个人与企业打造专属智能助手的首选。而Ollama作为轻量级本地大模型管理工具,能让OpenClaw彻底摆脱对云端大模型的依赖,实现“本地推理、数据不泄露、全流程私有化”的安全体验;同时阿里云提供的专属云端部署方案,可满足7×24小时稳定运行需求,兼顾隐私与便捷性。
717 2

热门文章

最新文章