· 7 years ago · Dec 14, 2018, 02:26 PM
1--- Ñоздаем и заполÑем таблицу
2
3DROP TABLE IF EXISTS test_symbol;
4
5CREATE TABLE `test_symbol ` (
6 `id` int(11) NOT NULL,
7 `letter` char(1) NOT NULL,
8 `prev_id` int(11) NOT NULL
9);
10
11INSERT INTO test_symbol VALUES (1, 'H', 3),(3, 'T', 0),(2, 'N', 6),(6, 'A', 1),(4, 'K', 2),(10, '_', 5),(5, 'S', 4),(8, 'G', 10),(9, 'D', 7),(7, 'O', 8);
12
13--- ÑобÑтвенно, решение:
14
15select group_concat(str.correct SEPARATOR '') as return_str from
16(
17 select distinct
18 l1.id,
19 if(l1.letter = '_', ' ', l1.letter) as correct
20 from
21 test_symbol l1 inner join test_symbol l2
22 having
23 ((select @next_id := ifnull(@next_id, (select id from test_symbol where prev_id=0))) is not null) and
24 (l1.id=@next_id) and
25 ((@next_id := (SELECT id FROM test_symbol WHERE prev_id=l1.id)) or (@next_id is null))) as str;