Php 替换列的复杂MySQL连接
保存内容信息的表-Php 替换列的复杂MySQL连接,php,mysql,join,Php,Mysql,Join,保存内容信息的表-内容: +----------+-------------+-------------------+--------------------------------+ | (int) id | (int) title | (int) description | (string) tags | +----------+-------------+-------------------+-------------------------------
内容
:
+----------+-------------+-------------------+--------------------------------+
| (int) id | (int) title | (int) description | (string) tags |
+----------+-------------+-------------------+--------------------------------+
| 1 | 12 | 18 | super, awesome, must-see |
+-----------------------------------------------------------------------------+
| 4 | 25 | 26 | randomness, funny-stuff, cool |
+-----------------------------------------------------------------------------+
保存翻译信息的表-翻译
:
+-----------+---------------------------------+----------------+
| (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.> |
+--------------------------------------------------------------+
到目前为止,我只需要为一个值连接翻译数据。。。是的,加入而不是替换:|
SELECT `content` . * , `translations`.value
FROM `content`
JOIN `translations` ON `translations`.tid = `content`.title
WHERE `translations`.code = 'en'
我如何做到这一点
提前谢谢 没那么复杂:
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')
基本上,您需要删除
*
,并从联接表中指定精确的列及其别名。第二件事是,您必须在同一个表上创建双连接,一个用于获取标题翻译,另一个用于获取描述。要在结果中“替换”它,只需使用value
列的别名。这一行:在translations
上加入translations
。tid=content
。title似乎是错误的:你必须在content.id上加入,而不是在title上加入……啊,谢谢,很有魅力。不知道您可以对语句进行分组。
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')