Answer the question
In order to leave comments, you need to log in
How to get value by index from comma separated list ("5,9,12")?
I have a table like this:
CREATE TABLE `category` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`parent_id` int(10) UNSIGNED DEFAULT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`),
CONSTRAINT `parent` FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`)
)
WITH RECURSIVE category_path AS
(
SELECT
c.parent_id,
c.id,
CONVERT(c.id, CHAR(1000)) AS ids
FROM category AS c
WHERE parent_id IS NULL
UNION ALL
SELECT
c.parent_id,
c.id,
CONCAT(p.ids, ',', c.id) AS ids
FROM category AS c
JOIN category_path AS p ON p.id = c.parent_id
)
SELECT
category_path.*
FROM category_path
ORDER BY category_path.ids
parent id ids
null 1 1
1 190 1,190
....
1 2 1,2
2 103 1,2,103
....
2 3 1,2,3
...
SUBSTRING_INDEX(SUBSTRING_INDEX(ids, ',', 1), ',', -1) + 0 ASC,
SUBSTRING_INDEX(SUBSTRING_INDEX(ids, ',', 2), ',', -1) + 0 ASC,
SUBSTRING_INDEX(SUBSTRING_INDEX(ids, ',', 3), ',', -1) + 0 ASC,
#...
REGEXP_REPLACE(category_path.ids, '^((([0-9]+,){0})([0-9]+)((,[0-9]+)*)|.*)$','\\4') + 0,
REGEXP_REPLACE(category_path.ids, '^((([0-9]+,){1})([0-9]+)((,[0-9]+)*)|.*)$','\\4') + 0,
REGEXP_REPLACE(category_path.ids, '^((([0-9]+,){2})([0-9]+)((,[0-9]+)*)|.*)$','\\4') + 0,
#...
WITH RECURSIVE category_path AS
(
SELECT
c.parent_id,
c.id,
CONVERT(LPAD(c.id, 11, '0'), CHAR(11000)) AS ids
FROM category AS c
WHERE parent_id IS NULL
UNION ALL
SELECT
c.parent_id,
c.id,
CONCAT(p.ids, ',', LPAD(c.id, 11, '0')) AS ids
FROM category AS c
JOIN category_path AS p ON p.id = c.parent_id
)
SELECT category_path.*
FROM category_path
ORDER BY category_path.ids;
parent id ids
null 1 0000000001
1 2 0000000001,0000000002
2 3 0000000001,0000000002,0000000003
3 4 0000000001,0000000002,0000000003,0000000004
4 5 0000000001,0000000002,0000000003,0000000004,0000000005
2 103 0000000001,0000000002,0000000103
103 104 0000000001,0000000002,0000000103,0000000104
104 105 0000000001,0000000002,0000000103,0000000104,0000000105
104 427 0000000001,0000000002,0000000103,0000000104,0000000427
103 353 0000000001,0000000002,0000000103,0000000353
353 354 0000000001,0000000002,0000000103,0000000353,0000000354
103 653 0000000001,0000000002,0000000103,0000000653
2 219 0000000001,0000000002,0000000219
....
Answer the question
In order to leave comments, you need to log in
Perhaps you should try using IN in the query. For example:
SELECT * FROM Table WHERE Field IN (5,9,12)
try replacing the mail() function with another one with settings, mb something on the server side
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question