Unión MySQL complicada con reemplazo de columnas

Table that holds content information - content:

+----------+-------------+-------------------+--------------------------------+
| (int) id | (int) title | (int) description | (string) tags                  |
+----------+-------------+-------------------+--------------------------------+
|        1 |          12 |                18 | super, awesome, must-see       |
+-----------------------------------------------------------------------------+
|        4 |          25 |                26 | randomness, funny-stuff, cool  |
+-----------------------------------------------------------------------------+

Table that holds translation information - translations:

+-----------+---------------------------------+----------------+
| (int) tid | (text) value                    | (varchar) code |
+-----------+---------------------------------+----------------+
|        12 | Super-awesome-mustsee           | en             |
+--------------------------------------------------------------+
|        18 | <here would be the description> | en             |
+--------------------------------------------------------------+
| <more translation data that is not necessary for this xmpl.> |
+--------------------------------------------------------------+

What I want to achieve is, to replace content.title con translations.value and same for description (more/less data for different components (content) tables) dónde content.title coincide con el translations.tid, me gusta:

+----------+-----------------------+---------------------------------+--------------------------------+
| (int) id | (text) title          | (text) description              | (string) tags                  |
+----------+-----------------------+---------------------------------+--------------------------------+
|        1 | Super-awesome-mustsee | <here would be the description> | super, awesome, must-see       |
+-----------------------------------------------------------------------------------------------------+

So far I've got to only join translation data for one value... And yes, join not replace. :|

SELECT  `content` . * ,  `translations`.value
FROM  `content` 
JOIN  `translations` ON  `translations`.tid =  `content`.title
WHERE  `translations`.code =  'en'

¿Cómo logro esto?

Gracias de antemano!

preguntado el 08 de noviembre de 11 a las 19:11

This line : JOIN translations ON translations.tid = content.title seems wrong: you must join on content.id, not on title... -

1 Respuestas

Not so complicated:

SELECT
    `c`.`id`,
    `tt`.`value` AS title,
    `td`.`value` AS description,
    `c`.`tags`
FROM
    `content` `c`
LEFT JOIN 
    `translations` `tt` ON  (`tt`.`tid` = `c`.`title` AND `tt`.`code` = 'en')
LEFT JOIN 
    `translations` `td` ON  (`td`.`tid` = `c`.`description` AND `td`.`code` = 'en')

Basically you need to drop * and specify exact columns and their aliases from joined tables. Second thing is that you yo have to create double join on the same table, one to get title translation, second to get description. To 'replace' it in result simply use the alias for value columna.

respondido 09 nov., 11:00

Ah, thanks, works like a charm. Didn't know that you can group ON declaraciones. - tomsseisums

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.