Domanda

I have a table field_list like this:

field_id|obj_id|field_key|field_content
----------------------------------------    
     1  |   1  |   123   |   Title_A
     2  |   1  |   234   |   IP_A
     3  |   1  |   567   |   DNS_A
     4  |   2  |   123   |   Title_B
     5  |   2  |   456   |   IP_B
     6  |   2  |   789   |   DNS_B
     7  |   3  |   123   |   Title_C
     8  |   3  |   456   |   IP_C
     9  |   3  |   789   |   DNS_C

What I need is this:

obj_id  |123    |456  |789
----------------------------------------    
    1   |Title_A|IP_A |DNS_A
    2   |Title_B|IP_B |DNS_B
    3   |Title_C|IP_C |DNS_C

I had several ideas, how to achieve this (create new table or view, use scirpts, etc.). Unfortunately my SQL-Skills are not very professional and I never know if I am working in the right direction.

Background: The mySql database is used for storing information about webpages. There is a table 'obj', and a table 'field_list'. 'obj_id' is foreign-key from obj table. 'field_id' is primary-key from field_list.

I managed to get something that looks what I want with concatenate and seperators, but not as result with many columns.

If there is no easy answer, or to many - perhaps someone can lead me into the right direction.

Please excuse my bad english and grammar. This is my first stackoverflow question - I hope you have patience with me. I will modify my question tomorrow if something is not clear.

THX!

È stato utile?

Soluzione

You could try something like this, but performance might not be perfect...

This table model excells in searching, not on it's table column representation:

SELECT object_id,
MAX( IF( field_key = 123, field_content, NULL) ) AS col_123,
MAX( IF( field_key = 234, field_content, NULL) ) AS col_234,
MAX( IF( field_key = 345, field_content, NULL) ) AS col_345,
MAX( IF( field_key = 456, field_content, NULL) ) AS col_456,
MAX( IF( field_key = 567, field_content, NULL) ) AS col_567
FROM field_list
GROUP BY object_id
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top