SELECT substring_index(substring_index( a.note,',',b.help_topic_id +1),',' ,-1) FROM (select'1,2,3,4'as note) a JOIN mysql.help_topic b ON b.help_topic_id < (length(a.note) - length( replace(a.note,',','')) +1)
变形,补行
1 2 3 4 5 6
SELECT a.startNum + b.help_topic_id AS rn FROM (select'001'AS startNum,'200'as endNumber) a JOIN mysql.help_topic b ON b.help_topic_id < (a.endNumber -a.startNum +1)
结合上面的行拆分
1 2 3 4 5 6
SELECT LPAD(a.startNum + b.help_topic_id,3,'0') AS rn FROM (select'001'AS startNum,'200'as endNumber) a JOIN mysql.help_topic b ON b.help_topic_id < (a.endNumber -a.startNum +1)
逗号隔开的数据关联查询
table2的PARAM_ID是逗号隔开的,要查询显示逗号隔开的table2.PARAM_NAME
1 2 3 4 5 6
SELECT GROUP_CONCAT( table2.PARAM_NAME ) FROM table2 WHERE table2.PARAM_ID REGEXP ( SELECT REPLACE ( ( SELECT table1.PARAM_ID FROM table1 WHERE table1.id ='1' ), ',', '|' ) )
行转列
逗号隔开的数据
1 2 3 4 5 6
SELECT SUBSTRING_INDEX( SUBSTRING_INDEX( '1,2,3,4', ',', help_topic_id +1 ), ',',-1 ) AS num FROM mysql.help_topic WHERE help_topic_id < LENGTH( '1,2,3,4' ) - LENGTH( REPLACE ( '1,2,3,4', ',', '' ) ) +1
非逗号隔开的数据
1
SELECTLEFT(SUBSTRING('QWER',help_topic_id+1),1) AS num FROM mysql.help_topic WHERE help_topic_id < LENGTH('QWER');
常用语法
获取某个表的某个字段信息
1
SELECT*FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='表名'and COLUMN_NAME='列名'
select*from performance_schema.threads where PROCESSLIST_COMMAND ='Query'orderby THREAD_OS_ID DESC
注:THREAD_OS_ID越高用的线程数越高
查看目前正在查询的耗时时间长的SQL
1
select*from information_schema.`PROCESSLIST` where Command !='Sleep'orderby `time` DESC
查看进程
1
SHOW PROCESSLIST;
统一修改字符集(生产修改的sql然后统一执行)
1 2 3 4 5 6
select distinct CONCAT("ALTER TABLE `", TABLE_NAME,"` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;") from information_schema.COLUMNS where TABLE_SCHEMA ='fit'and table_name like't_pom%' and (character_set_name !='utf8' or collation_name !='utf8_general_ci')
统一添加索引
1 2 3 4
select distinct CONCAT("ALTER TABLE `", TABLE_NAME,"` ADD INDEX `IX_DELETED` USING BTREE(`IS_DELETED`);") from information_schema.COLUMNS where TABLE_SCHEMA ='hra'
删除所有索引
1 2 3 4
select distinct CONCAT("DROP INDEX `IX_DELETED` on ", TABLE_NAME,";") from information_schema.COLUMNS where TABLE_SCHEMA ='hra_vn1'and table_name like'm_wms%'