SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist Fleeters's Avatar
    Join Date
    Jul 2003
    Location
    Dumpsville
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    selecting one random record from table

    So i'm working on this app that requires it to be updated randomly without user interaction. I need to be able to select 1 random record from a table where certain criteria is met

    heres the schema:

    CREATE TABLE `availibility` (
    `unitId` int(11) NOT NULL auto_increment,
    `unit` varchar(25) default NULL,
    `floor` varchar(25) default NULL,
    `status` varchar(50) NOT NULL,
    `bathrooms` tinytext,
    `bedrooms` tinytext,
    `sqft` varchar(50) NOT NULL default '',
    `size` varchar(50) NOT NULL default '',
    `isChanged` tinyint(1) NOT NULL default '0',
    PRIMARY KEY (`unitId`)
    )

    heres what the sql should do

    SELECT * FROM availibility WHERE floor='floorSpecified' AND status='1' LIMIT 1

    The problem is that it will return the same record every time. Is there a way to get random ones?
    Aaron Smith
    smithaaronlee.net

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT * FROM availibility WHERE floor='floorSpecified' AND status='1' ORDER BY RAND() LIMIT 1

  3. #3
    SitePoint Evangelist Fleeters's Avatar
    Join Date
    Jul 2003
    Location
    Dumpsville
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got it:

    SELECT * FROM " . $this->mysqltable . " WHERE floor = 'floor' AND status = '1' order by rand() LIMIT 1
    Aaron Smith
    smithaaronlee.net


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
  •