可以执行该SQL:GRANT SELECT ON mysql.help_topic TO 'autochain_uat'@'localhost'(给用户赋权限);
取消用户权限SQL:REVOKE SELECT ON mysql.help_topic FROM 'autochain_uat'@'localhost';
一、当前表结构
二、sql语句
SELECT
id,
product_name,
substring_index( substring_index( product.tags, ',', topic.help_topic_id + 1 ), ',',- 1 ) AS tag
FROM
tbl_product product
JOIN mysql.help_topic topic ON topic.help_topic_id < ( length( product.tags ) - length( REPLACE ( product.tags, ',', '' ) ) + 1 );
结果:
三、解析
1.Substring_index的作用:取得目标字符串左侧第n个分割符左侧的部分,n为负时返回右侧第n个的右部分
2.help_topic 是数据库mysql的一个表,该表提供查询帮助主题给定关键字的详细内容(详细帮助信息)
表字段含义:
- help_topic_id:帮助主题详细信息在表记录中对应的ID
- name:帮助主题给定的关键字名称。
- help_category_id:帮助主题类别ID,与help_category表中的help_category_id字段值相等。
- description:帮助主题的详细信息(这里就是我们通常查询帮助信息真正想看的内容,例如:告诉我们某某语句如何使用的语法与注意事项等)。
- example:帮助主题的示例信息(告诉我们语句如何使用的示例)。
- url:该帮助主题对应在MySQL官方在线手册中的URL链接地址。
ps:这条sql语句其实跟help_topic表没有什么关系,仅仅是借用了help_topic表的help_topic_id的值为(0,1,2,...),因此在本数据库中建立一个仅含ID字段的表进行关联,同样可以实现转多行效果,实践在没有mysql库权限的情况下,最好自己建表插入数据吧,如下:
drop table if exists `tmp_id`;
CREATE TABLE `tmp_id` (
`id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (0);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (1);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (2);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (3);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (4);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (5);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (6);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (7);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (8);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (9);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (10);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (11);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (12);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (13);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (14);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (15);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (16);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (17);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (18);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (19);
INSERT INTO `szkitil`.`tmp_id` (`id`) VALUES (20);
mysql> select help_topic_id,name from mysql.help_topic limit 5;
+---------------+------+
| help_topic_id | name |
+---------------+------+
| 502 | ! |
| 628 | != |
| 427 | % |
| 140 | & |
| 374 | * |
+---------------+------+
5 rows in set (0.00 sec)
辅助理解
mysql> select REPLACE ( '1,2,3', ',' , '');
+-----------------------------+
| REPLACE ( '1,2,3', ',', '') |
+-----------------------------+
| 123 |
+-----------------------------+
1 row in set (0.01 sec)
【MySQL】字符串截取之substring_index - 梦琪小生 - 博客园
mysql> select substring_index( "123,456", ',', 1 );
+--------------------------------------+
| substring_index( "123,456", ',', 1 ) |
+--------------------------------------+
| 123 |
+--------------------------------------+
1 row in set (0.01 sec)
mysql> select substring_index( "123,456", ',', 2 );
+--------------------------------------+
| substring_index( "123,456", ',', 2 ) |
+--------------------------------------+
| 123,456 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> select substring_index( "123,456", ',', -1 );
+---------------------------------------+
| substring_index( "123,456", ',', -1 ) |
+---------------------------------------+
| 456 |
+---------------------------------------+
1 row in set (0.01 sec)