I try to select rows and order them by RAND() and it works fine without any repetitions. But when i limit rows it keeps getting duplicated ones.
Let's say that i have a SHOW MORE button to get more results with AJAX and each click must change the limit (Where should rows start at and how many rows should be fetched). But Here i keep getting duplicated rows.
Here is my line:
SELECT * FROM products
ORDER BY RAND() LIMIT 20,20
I think that RAND() doesn't care whether LIMIT exists or not, it will always starts fetching rows at anywhere it likes randomly but limit how many rows must be fetched 😔.
Anyway is there anything to do with it?
keep id of already shown results in an variable and exclude it from select next time
SELECT * FROM products
where id not in (1, 7, 9) // id from variable
ORDER BY RAND() LIMIT 20,20
Related
I need to select say 2000000 records at random from a very large database. I looked at previous questions. So please do not mark this question as duplicate. I need clarification. Most answers suggest using ORDER BY RAND() function. So my query will be:
SELECT DISTINCT no
FROM table
WHERE name != "null"
ORDER BY RAND()
LIMIT 2000000;
I want each record to be selected at random. I am not sure if I understand the ORDER BY RAND() effect here. But I am afraid it will select a random record, say 3498 and will continue selection from there, say, the next records will be: 3499, 3500, 3501, etc.
I want each recor to be random, not to start the order from a random record.
How can I select 2000000 random record where each record is selected at random? Can you simplify what exactly ORDER BY RAND() does?
Note that I use Google BigQuery so the performance issue should not be a big problem here. I just want to achieve the requirement of selecting random 2000000 records.
SELECT x
FROM T
ORDER BY RAND()
is equivalent to
SELECT x
FROM (
SELECT x, RAND() AS r
FROM T
)
ORDER BY r
The query generates a random value for each row, then uses that random value to order the rows. If you include a limit:
SELECT x
FROM T
ORDER BY RAND()
LIMIT 10
This randomly selects 10 rows from the table.
SELECT *
FROM users
ORDER BY highscore DESC
LIMIT 5 OFFSET 5
and
SELECT *
FROM users
ORDER BY highscore DESC
LIMIT 5 OFFSET 10
returning the same result. And they are different than the records when I omit the LIMIT clause! I searched the community. There are similar questions, but they are of no help.
EDIT: Here are the table data-
Presumably, the issue is that you have ties for highscore. When you have ties, then MySQL orders the rows with the same value in an arbitrary and indeterminate way. Even two runs of the same query can result in different orderings.
Why? The reason is simple. There is no "natural" order for sorting keys with the same value. SQL tables represent unordered sets.
To make the sorting stable, include a unique id as the last key in the ORDER BY:
SELECT u.*
FROM users u
ORDER BY u.highscore DESC, u.userId
LIMIT 5 OFFSET 5;
Then, when you fetch the next 5 rows, they will be different.
I'm creating an advertisement platform for my website, where we need select 3 rows randomly and number of times of banner display also should be balanced.
We can get the random rows like this,
SELECT column FROM table ORDER BY RAND() LIMIT 0,3
and we can balance the number of times by incrementing count field while selecting the row each time and select the rows with less count like this,
SELECT * FROM table ORDER BY display_count LIMIT 0,3.
But it will return the values like 1,2,3,4,5,6 and so. But i need to select rows with minimum count randomly. Any suggestion or idea on this would be great?
is this what you are looking for:
SELECT *
FROM table
ORDER BY display_count ASC, RAND()
LIMIT 0,3;
Did you tried
SELECT column FROM table ORDER BY RAND(), display_count LIMIT 0,3
?
I think this is what you want.
Hey Guyz Is this possible to get total number of rows count with offset limit
Scenario
SELECT * FROM users limit 0,5;
This Query contain 300 records but the issue is if i call this query with offset the result will be show only 5 record and i don't want to write a Query in twice time. one for paging limit and other for total no of record count...
I don't want this
SELECT * FROM users limit 0,5; // paging
SELECT count(*) FROM users; // count
i have to merge this Queries or helps are definitely appreciated
You can use SQL_CALC_FOUND_ROWS like this
SELECT SQL_CALC_FOUND_ROWS * FROM users limit 0,5;
It gets the row count before applying any LIMIT clause. It does need another query to fetch the results but that query can simply be
SELECT FOUND_ROWS()
and hence you don't have to repeat your complicated query.
Is there a way to grab an exact amount of entries from a database example. For example say you had a table that just had an id and total visits for the columns. Say you wanted to grab exactly 20 entries and sort them by total visits. How would you go about this? I know how to sort the whole table, but would like to be able to grab the top twenty total visits and then sort them. Thanks
O and right now I am using sqlite, but I know in the future I will be using mysql also. Thanks
Try with:
SELECT * FROM TableName ORDER BY TotalVisits LIMIT 20
using limit to get the top 20,
and if you want to add another sort, add it after visit column
like :
SELECT * FROM mytable ORDER BY visits DESC
/*here put another order by field like date */
, date
LIMIT 20
Use ORDER - LIMIT clause
SELECT * FROM table ORDER BY field [ASC|DESC] LIMIT 20 OFFSET [offset value]
You need to use LIMIT, but you will need to put the whole thing in a subquery if you intend to re-sort the top 20 based on separate criteria. So
SELECT * from <table> order by <total visits column> LIMIT 20
will get you the top 20, but then to sort within that result you would do something like
SELECT * from
(SELECT * from <table> ORDER BY <total visits column> LIMIT 20)
ORDER BY <other criteria>
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
With one argument, the value specifies the number of rows to return from the beginning of the result set:
SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.
All on: http://dev.mysql.com/doc/refman/5.5/en/select.html better explnation for mysql, however sqlite works same way: http://www.sqlite.org/lang_select.html