1

I have a database structure. The issue is that the table is really complex (typo3).

I will simplify to this:

|     ID   |CAT  | 
|==========|=====|
|1         |44,15|
|2         |16   |
|3         |29,30|

What I want to have as a result is 1 row for each CATEGORY (CAT), it mean

1 44
1 15
2 16
3 29
3 30

I tried to use simple select but of course it does not work.

I also tried to use a left join with the table CATEGORY using CATEGORY.id IN (TABLE1.cat)

4

2 回答 2

0

如果 cat 中只有两个值:

select id, left(cat, locate(',', cat) - 1)
from t
where cat like '%,%'
union all
select id, substr(cat, locate(',', cat) + 1, length(cat))
from t
where cat like '%,%'
union all
select id, cat
from t
where cat not like '%,%'

如果您有类别列表,则可以执行以下操作:

select t.id, c.cat
from t join
     categories c
     on concat(',', c.cat, ',') like concat('%,', t.cat, ',%')
于 2013-03-01T16:51:53.493 回答
0

1.:你的数据库结构不好!它甚至不是第一范式(1NF),因为您在一列中有一个数组作为字符串表示。

2.:这不是那么棘手。

一些灵感:http ://blog.fedecarg.com/2009/02/22/mysql-split-string-function/或在这里:http ://dev.mysql.com/doc/refman/5.5/en/string- functions.html#function_substring-index

于 2013-03-01T16:50:55.030 回答