Oracle and PostgreSQL have nice techniques to generate a series of unique numbers, one per line.
Unfortunately MySQL doesn't, so create a view with a pre-defined number of rows and use it to join with the "post" table. In order to determine the number of rows in this view, which I will call "iterator", find out the maximum number of tokens in your "category" column.
As an example, if the "category" column has the value:
1,2,3,4,5,6,7
then it has 7 tokens i.e. 7 numbers that are separated by commas. Similarly, find out the maximum number of tokens you require.
Let's assume the maximum number of tokens is 5. Then the view definition looks like this -
create view iterator (x)
as
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5
;
Thereafter, your query should be straightforward.
--
select substring_index(substring_index(p.category,',',iter.x), ',', -1) as category,
p.title
from post p,
iterator iter
where iter.x <= length(p.category)-length(replace(p.category,',',''))+1
order by p.title, iter.x
;
A test run follows:
MariaDB [test]>
MariaDB [test]>
MariaDB [test]> -- Show the data in the "post" table
MariaDB [test]> select * from post;
+----------+---------+
| category | title |
+----------+---------+
| 7,3 | title 1 |
| 1,3 | title 2 |
| 1,2,3 | title 3 |
+----------+---------+
3 rows in set (0.00 sec)
MariaDB [test]>
MariaDB [test]> -- Show the contents of my script "test_qry.sql"
MariaDB [test]> \! cat -n test_qry.sql
1 --
2 select substring_index(substring_index(p.category,',',iter.x), ',', -1) as category,
3 p.title
4 from post p,
5 iterator iter
6 where iter.x <= length(p.category)-length(replace(p.category,',',''))+1
7 order by p.title, iter.x
8 ;
9
MariaDB [test]>
MariaDB [test]> -- Run the script "test_qry.sql"
MariaDB [test]> source test_qry.sql
+----------+---------+
| category | title |
+----------+---------+
| 7 | title 1 |
| 3 | title 1 |
| 1 | title 2 |
| 3 | title 2 |
| 1 | title 3 |
| 2 | title 3 |
| 3 | title 3 |
+----------+---------+
7 rows in set (0.00 sec)
MariaDB [test]>
MariaDB [test]>
MariaDB [test]>
Hope that helps.
(Please disregard the "MariaDB" prompt. It's just a fork of MySQL provided by Fedora 19. MySQL's acquisition by Oracle has raised concerns that it might become closed-source.)