IT部落

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 16|回復: 0

mysql拆分字符串作为查询条件

[複製鏈接]

7

主題

7

帖子

37

積分

部落NPC

Rank: 9Rank: 9Rank: 9

積分
37
發表於 2022-7-6 12:15:38 | 顯示全部樓層 |閱讀模式
[backcolor=rgba(255, 255, 255, 0.9)]有个群友问一个问题
[backcolor=rgba(255, 255, 255, 0.9)]
[backcolor=rgba(255, 255, 255, 0.9)]这表的ancestors列存放的是所有的祖先节点,以,分隔
[backcolor=rgba(255, 255, 255, 0.9)]例如我查询dept_id为103的所有祖先节点,现在我只有一个dept_id该怎么查
[backcolor=rgba(255, 255, 255, 0.9)]然后我去网上找到这样一个神奇的sql,改改表名就成了下面的这样
[color=rgba(238, 255, 255, 0.8)]sql
        SELECT                substring_index( substring_index( a.ancestors, ',', b.help_topic_id + 1 ), ',',- 1 ) AS shareholder         FROM                sys_dept a                JOIN mysql.help_topic b ON b.help_topic_id < ( length( a.ancestors ) - length( REPLACE ( a.ancestors, ',', '' ) ) + 1 )         WHERE        dept_id = 103
[backcolor=rgba(255, 255, 255, 0.9)]
[backcolor=rgba(255, 255, 255, 0.9)]嗯,没错结果出来了,然后我就很好奇,什么原理,一个个来看
mysql.help_topic
[backcolor=rgba(255, 255, 255, 0.9)]这个是一个mysql自带的帮助解释注释表,查询结果如下
[backcolor=rgba(255, 255, 255, 0.9)]
[backcolor=rgba(255, 255, 255, 0.9)]id从0开始,我这个版本最大id到584,版本不同应该id最大值也不一样,这个表的作用一会说
REPLACE
[backcolor=rgba(255, 255, 255, 0.9)]这个函数应该都知道吧,替换字符用的
[backcolor=rgba(255, 255, 255, 0.9)]
LENGHT
[backcolor=rgba(255, 255, 255, 0.9)]获取字符串的长度
[backcolor=rgba(255, 255, 255, 0.9)]
substring_index
[backcolor=rgba(255, 255, 255, 0.9)]查分字符串,三个参数,要拆分的字符串,根据拆分的字符,从第几个开始
[backcolor=rgba(255, 255, 255, 0.9)]如果最后的那个参数为正数则从左开始数,然后获取对应下标左边的所有字符
[backcolor=rgba(255, 255, 255, 0.9)]如果为负数,则从右边开始数,获取对应下标右边的所有字符串,这个就不演示了
[backcolor=rgba(255, 255, 255, 0.9)]
分析
[backcolor=rgba(255, 255, 255, 0.9)]
[backcolor=rgba(255, 255, 255, 0.9)]先来看第一段
[color=rgba(238, 255, 255, 0.8)]sql
( length( a.ancestors ) - length( REPLACE ( a.ancestors, ',', '' ) ) + 1 )
[backcolor=rgba(255, 255, 255, 0.9)]我们假设当前数据的ancestors值为 0,100,101 那么第一个length(a.ancestors)的值就是9 减去后面的一段
[backcolor=rgba(255, 255, 255, 0.9)]length( REPLACE ( a.ancestors, ',', '' ) ) 因为我们假设的值里面有两个 , 所以length为7 最后在加1 那么这段值为 3
[backcolor=rgba(255, 255, 255, 0.9)]和前面的 join on条件能查出的数据也就是mysql.help_topic这个表中所有id小于3的数据,也就是id为0,1,2的三条数据
[backcolor=rgba(255, 255, 255, 0.9)]那么现在先来看看这样查询的结果是啥
[backcolor=rgba(255, 255, 255, 0.9)]
[backcolor=rgba(255, 255, 255, 0.9)]那么我们假设现在是第一行,mysql.help_topic表中的help_topic_id为0
[color=rgba(238, 255, 255, 0.8)]sql
substring_index( substring_index( a.ancestors, ',', b.help_topic_id + 1 ), ',',- 1 )
[backcolor=rgba(255, 255, 255, 0.9)]最里面的substring_index 拆分后为0,因为没有能在拆分的了所以外面的substring_index返回的也是0
[backcolor=rgba(255, 255, 255, 0.9)]第二行help_topic_id为1的时候获取结果为0,100
[backcolor=rgba(255, 255, 255, 0.9)]然后执行外层的substring_index 根据,拆分,值为-1 所以从右边找一位,获取的值就是100
[backcolor=rgba(255, 255, 255, 0.9)]第三行结果为0,100,101,外层substring_index 执行后结果为101
[backcolor=rgba(255, 255, 255, 0.9)]....只能心中大喊牛逼
[backcolor=rgba(255, 255, 255, 0.9)]那么现在知道mysql.help_topic这个表的作用了吗?就是用来对拆分出的数据分行,专业点叫笛卡尔积 (真的不懂..)
[backcolor=rgba(255, 255, 255, 0.9)]这种方法也有缺点:就是拆出的行数不能大于mysql.help_topic这个表的数据条数

回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

QQ|Archiver|手机版|自動贊助|中国农垦

GMT+8, 2025-4-19 12:37 , Processed in 0.038140 second(s), 19 queries .

抗攻擊 by GameHost X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表
一粒米 | 中興米 | 論壇美工 | 設計 抗ddos | 天堂私服 | ddos | ddos | 防ddos | 防禦ddos | 防ddos主機 | 天堂美工 | 設計 防ddos主機 | 抗ddos主機 | 抗ddos | 抗ddos主機 | 抗攻擊論壇 | 天堂自動贊助 | 免費論壇 | 天堂私服 | 天堂123 | 台南清潔 | 天堂 | 天堂私服 | 免費論壇申請 | 抗ddos | 虛擬主機 | 實體主機 | vps | 網域註冊 | 抗攻擊遊戲主機 | ddos |