SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Threaded View

  1. #1
    Non-Member
    Join Date
    Jan 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Illegal grouping

    Hi,

    I am having a table with a numeric column where several rows can contain the same value. Now I am trying to determine a random value and then to get a random row from all rows with the next larger value.

    Basically I already got it working with the following pseudo code
    Code:
    // Getting a random value between 0 and the maximum value of field
    value = SELECT ROUND(RAND()*MAX(field)) FROM table
    
    // Getting the next larger value based on the random value
    value2 = SELECT field FROM table WHERE field>value ORDER BY field LIMIT 1
    
    // Getting one random row from all rows with the determined value
    row = SELECT * FROM table WHERE field=value2 ORDER BY RAND() LIMIT 1
    However I am wondering whether there is a better more efficient way to achive this.

    For example by trying to combine the first two calls into
    Code:
    SELECT field FROM table WHERE field>ROUND(RAND()*MAX(field)) ORDER BY field LIMIT 1
    I am getting an invalid grouping error.

    Thanks!

    Sorry, forgot to mention that this is about a MySQL database and hence probably went better into http://www.sitepoint.com/forums/forumdisplay.php?f=182
    Last edited by some1; Jan 31, 2006 at 05:55.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •