开发者

How to assign random number in each row of SELECT query?

I am fetching data using following query :

  select * from tbl_words where userID =开发者_StackOverflow社区 '3'

This select query will give data as it was entered into the database.

Here I want to assign another column for random numbers. So each row will have a random number in random number column.

How do I achieve this?


RAND() (in MySQL) generates a random number between 0 and 1. Multiply by 10 to get between 1 and 10, multiple by 100 for between 1 and 100... and so on.

Thanks to Null in the comments below, RANDOM() in SQLite generates a random number between negative-silliness and positive-silliness, so there's no need to multiply by anything for SQLite.

On insert:

INSERT INTO table_name (column1, column2, random_number) VALUES ("something", "else", RAND() * 100000)
INSERT INTO table_name (column1, column2, random_number) VALUES ("something", "else", RANDOM())

On select

SELECT column1, column2, RAND() * 100000 AS random_number FROM table_name
SELECT column1, column2, RANDOM() AS random_number FROM table_name


This could work:

select *, random() from tbl_words where userID = '3'


You need the random() function:

select t.*, random() from tbl_words t where userID = '3'


You can use random() in your query such as

Select * from `yourtablename` where `yourcolumnname` order by random()


SELECT random()
UNION ALL
select * from tbl_words where userID = '3'

Try it did not have resrouce to test it. Found Random from here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜