Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
860 views
in Technique[技术] by (71.8m points)

mysql - How to select ORDER BY column and RAND() both?

Hello dear friends.

mysql_query("SELECT id FROM tb_table ORDER BY num ASC, ORDER BY RAND() LIMIT 1");

is this coding correct? I want to find all rows ASC num and there can be 1000 rows that num is 1 another 1000 that num is 2. But I want it to sort ASC like 1s firstly and choose one of them randomly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You only need to specify ORDER BY once.

mysql_query("SELECT id FROM tb_table ORDER BY num ASC, RAND() LIMIT 1");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...