Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在两个表中查询多语言内容_Php_Mysql - Fatal编程技术网

Php 在两个表中查询多语言内容

Php 在两个表中查询多语言内容,php,mysql,Php,Mysql,我有两个表职位和翻译 桌柱 邮政编码 发布内容 翻译 邮政编码 语言 所容纳之物 这个数据库将为多种语言服务。 要获得一篇包含所有可用语言的文章,最好的查询是什么 input post_id = 2 output ->single row with this fields post_id = 2 post_content = 'lorem ipsum' post_content_en = 'english lorem ipsum' post_content_jp = 'japanes

我有两个表职位和翻译

桌柱

邮政编码 发布内容 翻译

邮政编码 语言 所容纳之物 这个数据库将为多种语言服务。 要获得一篇包含所有可用语言的文章,最好的查询是什么

input
post_id = 2

output 
->single row with this fields

post_id = 2
post_content = 'lorem ipsum'
post_content_en = 'english lorem ipsum'
post_content_jp = 'japanese lorem ipsum'
post_content_...= 'bla..bla..bla..' //the post_content_xx is language international code
我试过使用group_concat

SELECT a.post_id,GROUP_CONCAT(b.language,'--lang_content_separator--',b.content SEPARATOR '---main_separator---') AS all_languages
FROM posts a 
LEFT JOIN translation b ON a.post_id = b.post_id 
WHERE a.post_id = 2;
上面的查询正在运行,但我不喜欢它,因为我们必须设置group\u concat\u max\u len


是否有任何技巧或替代查询来替换该组\u concat?

我确信我的解决方案可以改进,但另一种方法是在PHP内部合并数组。对于SQL查询,您可以运行查询。查询中的联接似乎没有必要

SELECT post_id, language, content FROM translation WHERE post_id = 2
从PHP中,您可以使用以下代码检索和格式化数据

$result = mysql_query($query);

$post = array();
while ($row = mysql_fetch_array($result)) {
    $index = "post_content_" . $row['language'];
    $post['post_id'] = $row['post_id'];
    $post[$index] = $row['content'];
}

这需要在查询中完成,还是可以在PHP中完成?您可以很容易地编写一个函数,为您添加额外的字段,即$row['post_content_en']。我上面的查询也使用php通过拆分所有_语言来获得正确的结果。我想知道除了group_concatI之外还有什么其他选择吗?尝试使用2 query最小化查询您的解决方案。查询posts表和language表