0

I created a table consists of two columns, id and name.

create table City(id integer, name varchar(100));
insert into City(id, name) values (1, "Jakarta"), (2, "Bandung"), (3, "Tangerang"), (4, "Depok"), 
(5, "Surabaya"), (6, "Lampung"), (7, "Balikpapan"), (8, "Bogor");

I would like to pick a row randomly via the id. The code select floor(rand()*8) + 1 works fine. It does give random integers from 1 to 8.

The line

select * from City where id = floor(rand()*8) + 1

should always return one row only from City. But when I tried, it sometimes return more than one row, or even none. Why is this? thanks.

Arief
  • 199
  • 1
  • 12
  • duplication please see this post https://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql – Naor Tedgi Dec 02 '18 at 13:28

1 Answers1

0

Since you only have 8 cities in your table, you can just do:

select *
from City
order by rand()
limit 1
Jared C
  • 362
  • 7
  • 19