1

如何使用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

null如果少于 4 个 image_url如何显示以及如何区分?

4

1 回答 1

0
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
于 2012-09-10T09:51:41.417 回答