Reputation: 33
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.
Upvotes: 1
Views: 40
Reputation: 1310
you can use RAND() operand to do that
SELECT reg_number FROM users ORDER BY RAND() limit 1
Upvotes: 1