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;
大概这样