MYSQL RAND()不会生成正确的列表

时间:2016-06-26 15:36:38

标签: php mysql select

我从选择中获得正确的结果时遇到问题。表wp_rdp_category_images有3列:id(唯一) - category_name - category_image。

在下面的选择中,我需要最多4 x category_name和每个category_name,1 category_image。

有人可以帮我纠正这个选择吗?会很好的。提前谢谢。

这里是php代码:

SELECT "app_exam"."id",
       "app_exam"."name"
FROM   "app_exam"
WHERE  NOT ( "app_exam"."id" IN (SELECT V1."exam_id" AS Col1
                                 FROM   "app_examaction" V1
                                 WHERE  V1."id" IN (SELECT U0."id"
                                                    FROM   "app_examaction" U0
                                                    WHERE  ( U0."delegation_id"
                                                             = 1
                                                             AND U0."action" = t
                                                             AND U0."status" = s
                                                           ))) )  

我的SQL:

function show_game_category_images_func() {
    $content = '';
    $i = 1;
    $record = $GLOBALS['wpdb']->get_results('
    SELECT * FROM wp_rdp_category_images
    ORDER BY `category_name`, RAND()
    LIMIT 4 
    ');

    if($record){
        foreach($record as $key => $rec){                   
            // if($key == 'picture' && $rec != null && $rec != '')     
            $rec = '<img src="'.$upload_url.$record->category_image.'" width="150" height="150">';

            if ($i == 1) {
                echo '<span><img src="'.$upload_url.$rec->category_image.'" ></span>';  // even
            }
            if ($i == 2) {
                echo '<span><img src="'.$upload_url.$rec->category_image.'" ></span><br>';  // even
            }
            else {
                echo '<span><img src="'.$upload_url.$rec->category_image.'" ></span>';   // odd
            }
            $i++;
        }
    }   
    return $content;
}
add_shortcode('show_game_category_images', 'show_game_category_images_func');

这是一个测试表:

SELECT * FROM `wp_rdp_category_images`    
ORDER BY `category_name`, RAND() LIMIT 4

2 个答案:

答案 0 :(得分:3)

您可以使用此查询:

select     category_name,
           (select   category_image
            from     wp_rdp_category_images
            where    category_name = main.category_name
            order by rand()
            limit    1) category_image
from       wp_rdp_category_images main
group by   category_name
order by   rand()
limit      4

SQL Fiddle

如果您需要结果集中的所有列,请使用此变体:

select     main.*
from       (
            select     category_name,
                       (select   id
                        from     wp_rdp_category_images
                        where    category_name = main.category_name
                        order by rand()
                        limit    1) id
             from       wp_rdp_category_images main
            ) sub
inner join  wp_rdp_category_images main
        on  main.id = sub.id
group by    main.category_name
order by   rand()
limit      4

SQL Fiddle

PHP代码

您的PHP存在问题,以便重复第一行。请注意,当$i为1时,您将执行第一个if中的部分,但第二个else中的部分if }。

您需要在第二个else

之前加if
if ($i == 1) {
    echo '<span><img src="'.$upload_url.$rec->category_image.'" ></span>';  // even
} else if ($i == 2) { // !!!! ADDED "else" here!
    echo '<span><img src="'.$upload_url.$rec->category_image.'" ></span><br>';  // even
}
else {
    echo '<span><img src="'.$upload_url.$rec->category_image.'" ></span>';   // odd
}

但是您的评论似乎表明您想要制作两个专栏。在这种情况下,最好这样写:

echo '<span><img src="'.$upload_url.$rec->category_image.'" ></span>';  // all
if ($i % 2 == 0) echo '<br>';  // even only

答案 1 :(得分:0)

像随机ID

SELECT DISTINCT category_name, category_image
FROM `wp_rdp_category_images`
WHERE `id` > RAND() * (SELECT MAX(id) FROM `wp_rdp_category_images`)   
ORDER BY id
0 LIMIT 4

在代码中按category_name手动排序4个结果。