Ok so i need to ran a simple select from table limit 1 but i need the row either the first row or any row between 1-8.
I thought something like this, but then realised that there is no ID in the rows the only unique ID is the phone number
So i am wondering how would I limit 1 result but pick a random row from the first 1-8
Try with this,
SELECT * FROM
(SELECT * FROM tableName ORDER BY columnName LIMIT 0,7) as derivedTableName
ORDER BY RAND() LIMIT 0 , 1;
With sub query, you need to serialized what will be the 1st row and what will be 8th row.
Next take a random row from them.
Related
Suppose a SELECT query returns 10 rows. Is there any one line query such as this (which I tried but did not work) to select one random row from return results of a SELECT query -
select name from (select * from my_table where age > 10 ORDER BY age ASC AS rows)
ORDER BY RAND() LIMIT 1;
One approach is to do LIMIT RAND() and enclose this in another SELECT statement which does LIMIT 1.
Another approach is to add a new column to the table, initialize it with RAND(), and then select from it, ordering by the random column with LIMIT 1. You may even be able to do this on the fly, by JOINing your original table with another table consisting of a single column that takes values from RAND().
Your logic is correct, you just have the syntax wrong.
select name from (
select *
FROM my_table
where age > 10) AS rows
ORDER BY RAND()
LIMIT 1;
You were missing FROM in the subquery, and the alias for the subquery goes outside the parentheses.
DEMO
I'm trying to select a particular row in a database using SELECT and OFFSET. The result I get is logical and I do get the desired row I want. But then I need to UPDATE that same specific row so I do something like that:
UPDATE table
SET value=1
WHERE value IN (SELECT * FROM(
SELECT value FROM table WHERE some criteria LIMIT 1 OFFSET 2) temp_tab);
Now what I expect from this code is to UPDATE the selected row ONLY. Instead it Updates ALL rows in the datatable and sets their value to 1.
When I use only:
SELECT * FROM(
SELECT value FROM table WHERE some criteria LIMIT 1 OFFSET 2) temp_tab
I get only 1 row as the output. (LIMIT 1 OFFSET 2 makes sure I do get the 1 row and it's the 2nd available) I am not exactly sure what I am doing wrong or how I am supposed to achieve this.
Note: I do have to use SELECT and not some other method using unique ID of the row or something similar.
Any help would be greatly appreciated.
First, when using LIMIT and OFFSET you need to use ORDER BY as well. Otherwise the row you get is indeterminate.
One method uses LIMIT within the UPDATE itself. However, UPDATE doesn't allow OFFSET. So:
UPDATE table
SET value = 1
WHERE some criteria
ORDER BY ??
LIMIT 1;
The best method would use a unique id. You can do this with the double subquery approach:
UPDATE table
SET value = 1
WHERE id IN (SELECT id
FROM (SELECT id
FROM table
WHERE some criteria
ORDER BY ??
LIMIT 1 OFFSET 2
) t
);
If you don't have a single unique id, you can use multiple columns that uniquely define a single row.
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.
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
Is there a way in MySQL to have the first 10 result from a SELECT query skipped?
I'd like it to work something like LIMIT.
Use LIMIT with two parameters. For example, to return results 11-60 (where result 1 is the first row), use:
SELECT * FROM foo LIMIT 10, 50
For a solution to return all results, see Thomas' answer.
There is an OFFSET as well that should do the trick:
SELECT column FROM table
LIMIT 10 OFFSET 10
OFFSET is what you are looking for.
SELECT * FROM table LIMIT 10 OFFSET 10
From the manual:
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;
Obviously, you should replace 95 by 10. The large number they use is 2^64 - 1, by the way.
LIMIT allow you to skip any number of rows. It has two parameters, and first of them - how many rows to skip
To skip first 10 rows use OFFSET 10, but LIMIT is also required,
If you want to get all the rows and only skip first 10 rows try using a big number like 999999999 or as per your choice
SELECT * FROM table LIMIT 999999 OFFSET 10
select * from table where id not in (select id from table limit 10)
where id be the key in your table.
If your table has ordering by id, you could easily done by:
select * from table where id > 10