Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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
Mysql 将表的值放在多列中的一行中_Mysql - Fatal编程技术网

Mysql 将表的值放在多列中的一行中

Mysql 将表的值放在多列中的一行中,mysql,Mysql,我有两张这样的桌子: 后置图像 image_id| image_name | post_id 1 | hallo.jpg | 1 2 | morning.jpg | 1 3 | sun.jpg | 2 4 | star.jpg | 3 邮政表格 post_id | post_text 1 | hallo 2 | morning all 3

我有两张这样的桌子:

后置图像

image_id| image_name    | post_id  
1       | hallo.jpg     | 1  
2       | morning.jpg   | 1  
3       | sun.jpg       | 2
4       | star.jpg      | 3
邮政表格

post_id | post_text     
1       | hallo     
2       | morning all   
3       | sunlight       
4       | my night      
预期的结果是

post_text   | image_1   | image_2     | image_3 | image_4 | count_image
hallo       | hallo.jpg | morning.jpg | null    | null    | 2
morning all | sun.jpg   | null        | null    | null    | 1  
sunlight    | star.jpg  | null        | null    | null    | 1  
my night    | null      | null        | null    | null    | 0  

有人能帮助我,如何使用query创建输出吗?

将值放在单独的列中可能很棘手。也许一个带分隔符的字符串就足够了:

select p.post_id, p.post_text,
       group_concat(image_name) as images,
       count(i.post_id) as num_images
from post_table p left join
     post_image i
     on p.post_id = i.post_id
group by p.post_id, p.post_text;