# 这个实际上是命令行执行sql语句的命令 -e 参数指定要执行的sql语句 mysql -uroot -p -DdbName -e "SELECT t.chapter_num as num, t.title as title FROM chapter_info t where t.book_id = 10 order by t.chapter_num asc" > result.txt
导出json格式的数据
1 2 3 4 5 6 7 8 9
# 这个实际上是命令行执行sql语句的命令 -e 参数指定要执行的sql语句,输出结果为每行一个json格式,而不是整体是json格式,要转成完整的json格式,需要每行后面加个逗号,去掉第一行标题行,以及开头和结尾加上 {[]} mysql -uroot -p -DdbName -e "SELECT JSON_OBJECT('num', t.chapter_num, 'title', t.title, 'content',t.content) FROM chapter_info t where t.book_id = 10 order by t.chapter_num asc" > result.json 每行结尾加逗号:sed -i 's/"}/"},/' result.json 删除文件第一行:sed -i '1d' result.json 第一行上面插入{[ :sed -i '1i {[' result.json 最后一行下面插入]}:sed -i '$a ]}' result.json
警告⚠️
明明配置文件中已经配置了local_infile=ON,但是通过sql执行load data local infile时还是会报错呢?
这就说明客户端运行时没有找到默认的配置文件,所以还是建议把my.cnf放到/etc目录下吧 也可以在登录时加上--local-infile
1
mysql -uroot -p --local-infile
1 2 3 4 5 6 7 8 9 10 11 12
mysql> load data local infile '~/file.csv' ignore into table table1 FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';
# 跳过表头 mysql> load data local infile '~/file.csv' ignore into table table1 FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' ignore 1 lines;
# 指定字段 mysql> load data local infile '~/file.csv' ignore into table table1 FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' ignore 1 lines (字段1,字段2,字段3);