gpt4 book ai didi

mysql - 将表的值放在多列中的一行mysql

转载 作者:行者123 更新时间:2023-11-29 19:12:01 26 4
gpt4 key购买 nike

我有两个这样的表:

帖子图片

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

有人可以帮助我,如何使用查询创建该输出?

最佳答案

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

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;

关于mysql - 将表的值放在多列中的一行mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43012003/

26 4 0