开发者

Simple random query from database

开发者 https://www.devze.com 2023-04-10 10:38 出处:网络
I currently have an ads listing site on which there are 3 ordering filters. By default, the ads are sorted with the following line:

I currently have an ads listing site on which there are 3 ordering filters. 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. 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));

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!


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 ...

//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")


Use "RAND()" as the ORDER BY column.

0

精彩评论

暂无评论...
验证码 换一张
取 消