如何判断2个字符串逗号分割内容完全包含的,tidb没有自定义函数使用

问题 字段A 数据 : “1,2,3,4”
字段B 数据 : “3,1,2,4,7”
需求 字段A中每个元素都必须在字段B中出现 才认定是true 否则false
字段A和字段B中的数据是都是逗号分割且是乱序的。
由于tidb没有自定义函数可以使用,这个问题该如何处理,求救大佬

描述有误;
一个是前端传入的 B数据 “3,1,2,4,7”
A字段 ( “1,2,3,4”) 在数据库中已经写死了;

判断 字段A是否在数据B的子集 ,字段A的每个数据都出现在B数据中

1 个赞

先把第一个拆成4个元素,然后 FIND_IN_SET 应该就行了。

2 个赞

用sql太复杂了,放代码里实现比较简单

CREATE TABLE numbers (
num INT
);

– 插入0到100的数字
INSERT INTO numbers (num)
SELECT n FROM (
SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL
SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL
SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL
SELECT 25 UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL SELECT 29 UNION ALL
SELECT 30 UNION ALL SELECT 31 UNION ALL SELECT 32 UNION ALL SELECT 33 UNION ALL SELECT 34 UNION ALL
SELECT 35 UNION ALL SELECT 36 UNION ALL SELECT 37 UNION ALL SELECT 38 UNION ALL SELECT 39 UNION ALL
SELECT 40 UNION ALL SELECT 41 UNION ALL SELECT 42 UNION ALL SELECT 43 UNION ALL SELECT 44 UNION ALL
SELECT 45 UNION ALL SELECT 46 UNION ALL SELECT 47 UNION ALL SELECT 48 UNION ALL SELECT 49 UNION ALL
SELECT 50 UNION ALL SELECT 51 UNION ALL SELECT 52 UNION ALL SELECT 53 UNION ALL SELECT 54 UNION ALL
SELECT 55 UNION ALL SELECT 56 UNION ALL SELECT 57 UNION ALL SELECT 58 UNION ALL SELECT 59 UNION ALL
SELECT 60 UNION ALL SELECT 61 UNION ALL SELECT 62 UNION ALL SELECT 63 UNION ALL SELECT 64 UNION ALL
SELECT 65 UNION ALL SELECT 66 UNION ALL SELECT 67 UNION ALL SELECT 68 UNION ALL SELECT 69 UNION ALL
SELECT 70 UNION ALL SELECT 71 UNION ALL SELECT 72 UNION ALL SELECT 73 UNION ALL SELECT 74 UNION ALL
SELECT 75 UNION ALL SELECT 76 UNION ALL SELECT 77 UNION ALL SELECT 78 UNION ALL SELECT 79 UNION ALL
SELECT 80 UNION ALL SELECT 81 UNION ALL SELECT 82 UNION ALL SELECT 83 UNION ALL SELECT 84 UNION ALL
SELECT 85 UNION ALL SELECT 86 UNION ALL SELECT 87 UNION ALL SELECT 88 UNION ALL SELECT 89 UNION ALL
SELECT 90 UNION ALL SELECT 91 UNION ALL SELECT 92 UNION ALL SELECT 93 UNION ALL SELECT 94 UNION ALL
SELECT 95 UNION ALL SELECT 96 UNION ALL SELECT 97 UNION ALL SELECT 98 UNION ALL SELECT 99 UNION ALL
SELECT 100
) AS nums;

– 查询 numbers 表,验证数字序列是否生成成功
SELECT * FROM numbers;

CREATE TABLE test_data (
A_id INT,
A_data VARCHAR(100),
B_id INT,
B_data VARCHAR(100)
);

– 插入数据
INSERT INTO test_data (A_id, A_data, B_id, B_data) VALUES
(1, ‘1,2,3,4’, 1, ‘3,1,2,4,7’),
(2, ‘5,6,7’, 2, ‘6,8,9,10’);

WITH A_elements AS (
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(A_data, ‘,’, num), ‘,’, -1) AS element,
A_id
FROM test_data
JOIN numbers
ON num <= LENGTH(A_data) - LENGTH(REPLACE(A_data, ‘,’, ‘’)) + 1
),
– 以字段B为基础,将其拆分成单独的行
B_elements AS (
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(B_data, ‘,’, num), ‘,’, -1) AS element,
B_id
FROM test_data
JOIN numbers
ON num <= LENGTH(B_data) - LENGTH(REPLACE(B_data, ‘,’, ‘’)) + 1
)
– 计算A中的每个元素是否都在B中出现
SELECT
A_id,
CASE WHEN COUNT(B_elements.element) = COUNT(A_elements.element) THEN ‘true’ ELSE ‘false’ END AS result
FROM A_elements
LEFT JOIN B_elements ON A_elements.element = B_elements.element
GROUP BY A_id
ORDER BY 1;
大概这样

3 个赞

必须在 db 里完成吗?感觉在代码里,排序 + md5 比较的方式比较方便呢

逻辑问题放在代码中,
db 只做读写

1 个赞

大神,描述改了

我们业务是迁移到tidb处理,之前有个自己写的函数用的, 迁移为了解决速度问题

我们业务是迁移到tidb处理,之前有个自己写的函数用的, 迁移为了解决速度问题

:thinking:感觉没什么好方案,我们一般是在应用层去解析拼接sql

1 个赞

代码解决更好

用代码来实现,TiDB不支持自定义函数

tidb判断太复杂了吧

代码实现吧~

建议代码实现

tidb不支持,好像已经在做了,等吧,要不就代码处理

这种需求,最好还是在前端代码中实现吧。即使在传统的关系型数据库中,即便有函数和存储过程,这些逻辑在数据库端实现的效率也是不高的。

SQL是有极限的

来学习一下

这个难度还不是sql的极限