gpt4 book ai didi

mysql - 如何使用mysql随机将行转换为列

转载 作者:行者123 更新时间:2023-11-30 23:27:45 24 4
gpt4 key购买 nike

如何使用mysql随机将行转换为列?不同的 (image_url)

 select p.email , 
image_url AS image_url_1,
image_url AS image_url_2 ,
image_url AS image_url_3,
image_url AS image_url_4
FROM
poll p
GROUP BY
email
ORDER by RAND()

此外,在这个查询中

select email,
(select IFNULL((image_url from poll p1 order by rand() limit 1), NULL) as image_1,
(select IFNULL((image_url from poll p2 order by rand() limit 1), NULL) as image_2,
(select IFNULL((image_url from poll p3 order by rand() limit 1), NULL) as image_3,
(select IFNULL((image_url from poll p4 order by rand() limit 1), NULL) as image_4
from poll
group by email

当image_url少于4个时,如何显示null,如何区分?

最佳答案

select email,
(select image_url from poll p1 order by rand() limit 1) as image_1,
(select image_url from poll p2 order by rand() limit 1) as image_2,
(select image_url from poll p3 order by rand() limit 1) as image_3,
(select image_url from poll p4 order by rand() limit 1) as image_4
from poll

关于mysql - 如何使用mysql随机将行转换为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12348928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com