简体   繁体   English

来自数据库的简单随机查询

[英]Simple random query from database

I currently have an ads listing site on which there are 3 ordering filters. 我目前有一个广告列表网站,上面有3个订购过滤器。 By default, the ads are sorted with the following line: 默认情况下,广告按以下行排序:

    $this->setState('filter_order', $app->getUserStateFromRequest('com_adsman.filter_order','filter_order', "start_date"));

When I change the start_date to another column name, it sorts by that column by default, so I know that's where the change needs to be done. 当我将start_date更改为另一个列名时,默认情况下它将按该列排序,因此我知道这是需要进行更改的地方。 Now, how would I go about displaying random results, based on the above piece of code? 现在,基于上述代码,我将如何显示随机结果?

Thanks! 谢谢!

Edit: Here is where the actual query is called. 编辑:这是调用实际查询的位置。

            $Orderings[]    = "`a`.$filter_order $filter_order_Dir";
    $Orderings[]    = "`a`.`id` $filter_order_Dir ";        

$query = " SELECT ".implode(",",$SelectCols)." \r\n ".

     " FROM `#__ads` AS `a` \r\n".

     implode(" \r\n ",$JoinList)."\r\n".

     $where."\r\n".

     " GROUP BY `a`.`id` ".

     " ORDER BY ".implode(",",$Orderings)." \r\n "; 

I'm thinking of using something like 我正在考虑使用类似

$rand = rand(.implode(",",$SelectCols)); $ rand = rand(.implode(“,”,$ SelectCols));

and changing $filter_order to $rand..I know this is not going to work tho, wrong syntax and wrong everything, this is where I need help! 并将$ filter_order更改为$ rand ..我知道这将无法正常工作,语法错误,所有内容均不正确,这是我需要帮助的地方!

well, since the query is created somewhere else, and only filled with the parameters coming from this line ... you can't just change this line ... 好吧,因为查询是在其他地方创建的,并且只填充了此行中的参数...您不能只更改此行...

find the actual query ... append a new column like "rand() as random" and change "start_date" to "random" in this line ... 找到实际的查询...在此行中添加新列,例如“ rand()as random”,然后将“ start_date”更改为“ random” ...

//edit: //编辑:

$query = " SELECT ".implode(",",$SelectCols).",rand() as random \r\n ".
     " FROM `#__ads` AS `a` \r\n".
     implode(" \r\n ",$JoinList)."\r\n".
     $where."\r\n".
     " GROUP BY `a`.`id` ".
     " ORDER BY ".implode(",",$Orderings)." \r\n "; 

should give you a random column named "random" (of course, if there is already a column with that name, that would be trouble ... so maybe choose a unique name instead of "random") 应该给您一个随机的列命名为“ random”(当然,如果已经有一个具有该名称的列,那将会很麻烦……所以也许选择一个唯一的名称而不是“ random”)

使用"RAND()"作为ORDER BY列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM