1

I have a table with the following structure and these values in column assoc.

| id | assoc |

| 1 | |3|-1|107|-4|146|-6| |

| 2 | |19|-3|107|-5| |

| 3 | |42|-1| |

You can see it here

This is a wrong mysql table structure. So I think that the right structure it must be:

| id | assoc | attrib | order |

| 1  | 3 | 1 | 1 |

| 1  | 107 | 4 | 2 |

| 1  | 146 | 6 | 3 |

| 2  | 19 | 3 | 1 |

| 2  | 107 | 5 | 2 |

| 3  | 42 | 1 | 1 |

Is possible to do with a mysql script on phpmyadmin?

2
  • “How it can be converted?” – for example by a script that you write for that purpose … Commented Mar 23, 2014 at 17:18
  • Do you know the script which can do it? Commented Mar 23, 2014 at 19:44

1 Answer 1

1
SET @prev := null;

SET @cnt := 0;

SELECT id,blah,mah,IF(@prev <> id, @cnt := 1, @cnt := @cnt + 1) AS rank, @prev := id 
FROM(
SELECT id,REPLACE(SUBSTRING_INDEX(assoc,'|',2),'|','')*1 as blah, 
SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(assoc,'-0'),'|',3),'-',-1)as mah FROM table1
UNION ALL
SELECT id,
SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(assoc,'|0'),'|',4),'|',-1)*1 as blah,
SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(assoc,'-0'),'|',5),'-',-1)as mah FROM table1
UNION ALL
SELECT id,
SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(assoc,'|0'),'|',6),'|',-1)*1 as blah,
SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(assoc,'-0'),'|',7),'-',-1)as mah FROM table1
)x
WHERE   x.mah !='0'
ORDER BY x.id ,x.blah 

FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

@user3450939 it would be nice if you accepted the answer(click under the score arrows)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.