MXBLOG - mysql
type: channel
link: https://moonjer.com/tag/mysql/
items: mysql8导入导出数据库
items: 在特定数据库中查找字段
items: mybatis常用SQL
items: mysql更新移除逗号分割字符串中部分字符
items: mysql常用命令
items: 使用explain+SQL语句来模拟优化器执行SQL查询语句
items: docker安装mysql
items: 小说站-维护-清理无效小说存储

mysql8导入导出数据库
type: item
date: 2021-12-25T17:56:00+08:00
description: 1.连接数据库mysql -u root -p远程数据库mysql -u root -p -h 192.168.1.100 -P 33072.导出mysqldump -u 用户名 -p 数据库名>/mnt/usb/testdb.sqlmysqldump -u root -p testdb>/mnt/usb/testdb.sql远程数据库mysqldump -h 主机 -P 端口 -u 用户名 -p 数据库名>/mnt/usb/testdb.sqlmysqldump -h 192.168.1.100 -P 3307 -u root -p testdb>/mnt/usb/testdb.sql3.导入mysql -u 用户名 -p 数据库名</mnt/usb/testdb.sqlmysql -u root -p testdb</mnt/usb/testdb.sql4.定制导出脚本USER_NAME=$1 if [ ! -n "$USER_NAME" ]; then echo "User name can't be empty!" exit 0 fi PWD=$2 if [ ! -n "$PWD" ]; then echo "Password name can't be empty!" exit 0 fi echo "Password is $PWD" DB_NAME=$3 if [ ! -n "$DB_NAME" ]; then echo "Database name can't be empty!" exit 0 fi PORT=$4 if [ ! -n "$PORT" ]; then PORT=3306 fi BACK_PATH=$5 if [ ! -n "$BACK_PATH" ]; then BACK_PATH='./db-back' fi HOST=$6 if [ ! -n "$HOST" ]; then HOST='127.0.0.1' fi BACK_FILE_NAME=_"$PORT"_$(date '+%Y%m%d%H%M%S') if [ -a $BACK_PATH ]; then echo "Back path is '$BACK_PATH'." else mkdir $BACK_PATH fi cd $BACK_PATH mysqldump -h $HOST -P $PORT -u$USER_NAME -p$2 $DB_NAME>$DB_NAME$BACK_FILE_NAME.sqldemosh backup.sh root 111111 dbname 3306 /mnt/usb/db-backup

在特定数据库中查找字段
type: item
date: 2021-12-06T18:01:16+08:00
description: 在整个MySQL数据库中查找select * from INFORMATION_SCHEMA.columns where COLUMN_NAME Like '%placement%';在特定数据库中查找SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('ColumnA','ColumnB') AND TABLE_SCHEMA='DatabaseName'; SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%ColumnA%' AND TABLE_SCHEMA='DatabaseName';在特定数据库中查找包含A和B字段的表SELECT DISTINCT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('A') and TABLE_NAME in (SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('B') AND TABLE_SCHEMA='DatabaseName')

mybatis常用SQL
type: item
date: 2021-10-21T16:32:41+08:00
description: 1.SET UPDATE t_user status =#{status}, run_status =#{runStatus}, WHERE id=#{id} 2.DELETE/删除指定条数 DELETE from t_user WHERE id = #{id} LIMIT #{num} 3.choose-when结构/IFNULL/MAX/ INSERT INTO t_robot_chat_expand (chat_word_id,sort, content ) VALUES( #{chatWordId}, (SELECT IFNULL(MAX( RCE.sort ) + 1,0) FROM t_robot_chat_expand RCE WHERE RCE.chat_word_id = #{chatWordId} ), IF((SELECT MAX( RCE.sort ) FROM t_robot_chat_expand RCE WHERE RCE.chat_word_id = 1 ) < #{sort}, (SELECT MAX( RCE.sort ) + 1 FROM t_robot_chat_expand RCE WHERE RCE.chat_word_id = #{chatWordId} ), #{sort}), #{content})

mysql更新移除逗号分割字符串中部分字符
type: item
date: 2021-10-21T15:49:28+08:00
description: UPDATE t_robot_team SET chat_words = TRIM(BOTH ',' FROM REPLACE( CONCAT( ',', chat_words, ',' ), CONCAT( ',', 21, ',' ), ',' )) WHERE locate(CONCAT(',',21,','),CONCAT(',',chat_words,','));

mysql常用命令
type: item
date: 2021-10-21T15:47:00+08:00
description: 1.从某张表随机取一条记录简单，低效SELECT * FROM table_name ORDER BY RAND() LIMIT 1；速度快，效率高SELECT t1.id, t1.word, t1.STATUS FROM hy_idiom AS t1 JOIN ( SELECT ROUND( RAND() * (( SELECT MAX( id ) FROM hy_idiom WHERE STATUS = 1 )-( SELECT MIN( id ) FROM hy_idiom WHERE STATUS = 1 ))+ ( SELECT MIN( id ) FROM hy_idiom WHERE STATUS = 1 ) ) AS id ) AS t2 WHERE t1.id >= t2.id AND t1.STATUS = 1 ORDER BY t1.id LIMIT 5;SELECT T1.* FROM ( SELECT (@RN := @RN + 1) AS sort, U.* FROM t_user U, (SELECT @RN := -1) AS T11 ) T1 JOIN( SELECT FLOOR( rand()*( SELECT COUNT(1) FROM t_user ) ) AS sort ) T2 ON T1.sort = T2.sort2.从表总记录数取随机序号SELECT FLOOR( rand()*( SELECT COUNT(*) FROM t_user ) );3.显示某张表序号/行号SELECT (@RN := @RN + 1) AS sort, id FROM t_user, (SELECT @RN := -1) AS T0

使用explain+SQL语句来模拟优化器执行SQL查询语句
type: item
date: 2021-10-13T21:04:30+08:00
description: mysql执行计划​ 在企业的应用场景中，为了知道优化SQL语句的执行，需要查看SQL语句的具体执行过程，以加快SQL语句的执行效率。​ 可以使用explain+SQL语句来模拟优化器执行SQL查询语句，从而知道mysql是如何处理sql语句的。​ 官网地址： https://dev.mysql.com/doc/refman/5.5/en/explain-output.html1、执行计划中包含的信息ColumnMeaningidThe SELECT identifierselect_typeThe SELECT typetableThe table for the output rowpartitionsThe matching partitionstypeThe join typepossible_keysThe possible indexes to choosekeyThe index actually chosenkey_lenThe length of the chosen keyrefThe columns compared to the indexrowsEstimate of rows to be examinedfilteredPercentage of rows filtered by table conditionextraAdditional informationidselect查询的序列号，包含一组数字，表示查询中执行select子句或者操作表的顺序id号分为三种情况：​ 1、如果id相同，那么执行顺序从上到下explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal;​ 2、如果id不同，如果是子查询，id的序号会递增，id值越大优先级越高，越先被执行explain select * from emp e where e.deptno in (select d.deptno from dept d where d.dname = 'SALES');​ 3、id相同和不同的，同时存在：相同的可以认为是一组，从上往下顺序执行，在所有组中，id值越大，优先级越高，越先执行explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal where e.deptno in (select d.deptno from dept d where d.dname = 'SALES');select_type主要用来分辨查询的类型，是普通查询还是联合查询还是子查询select_type ValueMeaningSIMPLESimple SELECT (not using UNION or subqueries)PRIMARYOutermost SELECTUNIONSecond or later SELECT statement in a UNIONDEPENDENT UNIONSecond or later SELECT statement in a UNION, dependent on outer queryUNION RESULTResult of a UNION.SUBQUERYFirst SELECT in subqueryDEPENDENT SUBQUERYFirst SELECT in subquery, dependent on outer queryDERIVEDDerived tableUNCACHEABLE SUBQUERYA subquery for which the result cannot be cached and must be re-evaluated for each row of the outer queryUNCACHEABLE UNIONThe second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY)--sample:简单的查询，不包含子查询和union explain select * from emp; --primary:查询中若包含任何复杂的子查询，最外层查询则被标记为Primary explain select staname,ename supname from (…

docker安装mysql
type: item
date: 2021-09-22T21:26:00+08:00
description: 忽略大小写8.0.26docker run -d --name=db3307 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=111111 mysql:8.0.26 --lower_case_table_names=15.7.35docker run -d --name=db3307 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=111111 mysql:5.7.35 --lower_case_table_names=1需要挂载目录的话：docker run -p 3307:3306 --name=db3307 \ --privileged=true \ --restart unless-stopped \ -v /opt/docker/mysql/conf:/etc/mysql \ -v /opt/docker/mysql/logs:/logs \ -v /opt/docker/mysql/data:/var/lib/mysql \ -v /etc/localtime:/etc/localtime \ -e MYSQL_ROOT_PASSWORD=111111 \ -d mysql:8.0.26 \ --lower_case_table_names=1创建数据库命令CREATE DATABASE `testdb` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_0900_ai_ci';解决问题Caused by: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'cpms_v2.cpms_project.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_bySHOW VARIABLES LIKE '%case%'; SET GLOBAL sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

小说站-维护-清理无效小说存储
type: item
date: 2021-08-27T15:01:00+08:00
description: 1.清理指定id的小说所有内容DROP PROCEDURE IF EXISTS clear_invalid_novel; CREATE PROCEDURE clear_invalid_novel() BEGIN DECLARE bookid varchar(255) DEFAULT '1431094810327994368'; DELETE FROM book_content WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content0 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content1 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content2 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content3 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content4 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content5 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content6 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content7 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content8 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_content9 WHERE index_id in (SELECT id FROM book_index WHERE book_id=bookid); DELETE FROM book_index WHERE book_id=bookid; DELETE FROM book WHERE id=bookid; END; call clear_invalid_novel();2.删除无效的小说文本delete from book_content where index_id not in(SELECT id from book_index); delete from book_content0 where index_id not in(SELECT id from book_index); delete from book_content1 where index_id not in(SELECT id from book_index); delete from book_content2 where index_id not in(SELECT id from book_index); delete from book_content3 where index_id not in(SELECT id from book_index); delete from book_content4 where index_id not in(SELECT id from book_index); delete from book_content5 where index_id not in(SELECT id from book_index); delete from book_content6 where index_id not in(SELECT id from book_index); delete from book_content7 where index_id not in(SELECT id from book_index…