1

I have following mysql table.

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `reg_number` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;

In this table there are some values as follows

INSERT INTO `users` (`name`, `reg_number`) VALUES('qaser', 'j-001'), ('rizwan', 'j-002'), ('rizwan', 'j-003'),('rizwan', 'j-004'),('rizwan', 'j-005'),('rizwan', 'j-006'),('rizwan' 'j-007'),('rizwan', 'j-008');

I am retrieving these values using the following query

$sql="SELECT reg_number FROM users";
    $result=mysqli_query($con,$sql);
    if(!$result){
            die(mysqli_error($con));
           }
    while($row=mysqli_fetch_array($result)){
    echo $row['reg_number'];
}

It gives me the following output

j-001 j-002 j-003 j-004 j-005 j-006 j-007 j-008

It goes very well as the these queries run. I want random output of this result which will be only one value eg. j-005

as the rand(1,8) function work it gives only one value.

4
  • SELECT reg_number FROM users ORDER BY RAND() LIMIT 1? Commented Aug 31, 2014 at 21:56
  • @Class I have used it but show all values randomly. I want only one value from these to show on the page. Commented Aug 31, 2014 at 22:08
  • did you also use LIMIT 1 because it limits the results of the search Commented Aug 31, 2014 at 22:10
  • @Class oh sorry I forgot that thank you very much you make my life easy. Commented Aug 31, 2014 at 22:15

1 Answer 1

1

you can use RAND() operand to do that

SELECT reg_number FROM users ORDER BY RAND() limit 1
Sign up to request clarification or add additional context in comments.

1 Comment

I have used it but show all values randomly. I want only one value from these.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.