Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 376897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:38:38+00:00 2026-05-12T14:38:38+00:00

Alright here’s the situation, I have an application written in the Zend_Framework, that is

  • 0

Alright here’s the situation, I have an application written in the Zend_Framework, that is compatible with both MySQL and MSSQL as the backend. Now, ZF is pretty good at solving a lot of the SQL discrepancies/differences between the two languages, but I still have yet to figure this one out.

The objective is to select 1 random record from the table, which is an extremely simple statement.

Here’s a select statement for example:

$sql = $db->select()
      ->from("table")
      ->order("rand()")
      ->limit(1);

This works perfectly for the MySQL database tables, because the sql for MySQL is as follows:

SELECT `table`.* FROM `table` ORDER BY rand() ASC

Now MSSQL on the other hand, uses the newid() function to do randomizing.

Is there some sort of helper I can pass into the order() function in order to make it realize that it has to use the proper ordering? I searched the documentation and on the zfforums, found a few tips, but nothing solid.

One of the things I did find was:

ORDER BY RANDOM() not working – ZFForums.com

They are using the following:

$res = $db->fetchAll(
'SELECT * FROM table ORDER BY :random',
array('random' => new Zend_Db_Expr('RANDOM()')
);

It works… but I am not looking to build my select statement by typing it out and doing a replace on the string, I am trying to keep it in the same Zend_Db_Select object. I also have tried passing in the Zend_Db_Expr('RANDOM()') into the ->order() on the statement, and it fails. He also posts a theoretical solution to finding the answer, but I am not looking to rewrite the function this is within, modifying the $db->fetch() call.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:38:38+00:00Added an answer on May 12, 2026 at 2:38 pm

    You could quickly abstract the function to a table – who knows which adapter it is using:

    class MyTable extends Zend_Db_Table_Abstract {
       public function randomSelect($select=null) {
         if ($select === null) $select = $this->select();
         if (!$select instanceOf Zend_Db_Select) $select = $this->select($select);
         $adapter = $this->getAdapter();
         if ($adapter instanceOf Zend_Db_Adapter_Mysqli) {
           $select->order(new Zend_Db_Expr('RAND()'));
         } else if ($adapter instanceOf Zend_Db_Adapter_Dblib) {
           $select->order(new Zend_Db_Expr('NEWID()'));
         } else { 
           throw new Exception('Unknown adapter in MyTable');
         }
         return $select;
      }
    }
    
    $someSelect = $table->select();
    // add it to an existing select
    $table->randomSelect($someSelect);
    
    // or create one from scratch
    $select = $table->randomSelect();
    

    Also, I found an article somewhere which I lost that recommended trying something like:

    $select->order(new Zend_Db_Expr('0*`id`+RAND()));
    

    to subvert MSSQL’s query optimizer and trick it into calculating a new value for each row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I have a strange one here. We currently have a tools application that
Alright here is what I am attempting to do. I have a form written
Alright, here's my dillema. I am making a simple application with Qt Creator that
Alright, here we go. I have a cocos2d app, and there are targets that
Alright, so here is the situation... Say I have a navbar for a site,
Alright here is the thing, I have this site that was once wordpress but
Alright so here is the deal. I am trying to make a application that's
Alright, possible a naive question here. I have a service that needs to log
Alright so here it goes, I have my user system working perfectly, and now
Alright so here is the question. I have a user class which contains a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.