douduan5086 2014-05-27 18:15
浏览 196
已采纳

使用SQL SELECT查询返回最少数量的用户

I'm a little new to using SQL and was wondering if someone might be able to help me with this query I'm trying to figure out. I'm developing a Wordpress theme and something I'd like to do in it is generate a random set of users on a page by using a SELECT query. It works great right now, I have it randomly pulling users from the database and displaying them with the right formatting. I can even limit the number of users (using the LIMIT clause) or order them by user properties.

However, my challenge is in getting the query to return at least 4 users (sometimes RAND() function causes only 2 to be displayed, which isn't great for a front-facing page).

I don't want to create a query that will throw an error if there are less than four users (which there will never be, but I'd still want to write clean code).

So, in summary: SELECT query that returns exactly 4 results, randomly ordered and, and doesn't throw an error if there's less than four users.

Any thoughts? I've tried using a few different ways to give RAND() different parameters and looked to see if there's a 'minimum' clause, but I haven't found anything. It's also a bit of a strange query bc most of the time we're more concerned with upper than lower limits.

Thanks everyone!

Here's the code I have in a custom.php file (I'm using the Roots starter theme):

function wpb_random_users() {

global $wpdb;


// Query database for users, change the number of users displayed by changing the limit
$usernames = $wpdb->get_results("SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY RAND() LIMIT 4 ");

// Display users in a list
foreach ($usernames as $username) {

if (!$username->user_url) :
$randomusers .= '<div class="col-md-3">'.'<img class="img-circle img-responsive" src="'.get_wp_user_avatar_src($username->user_email, 'thumbnail').'" alt="" />'.'<h4>'.$username->user_nicename.'</h4>'.'</div>';

endif;
}

return $randomusers;
}

add_shortcode('randomusers','wpb_random_users');
  • 写回答

3条回答 默认 最新

  • dpfad62426 2014-05-27 18:25
    关注

    The SQL query is not the reason you're getting less than 4 results... You should get 4 every time, provided there's at least 4 rows in the table.

    I suggest you debug this line: if (!$username->user_url) :. You are displaying only users with no URL. So, if you get 4 users back from the SQL query, but only two of them lack a URL, you only get 2 users.

    Incidentally, you don't need to seed the rand() function for what you're doing. In fact, it's less desirable. Just use SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY RAND() LIMIT 4 ");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型