Is it available to write a query to use same "LIMIT (from), (count)", but get result in backwards?
In example if I have 8 rows in the table and I want to get 5 rows in two steps I would:
first step query:
select * from table limit 0, 5
first step result:
first 5 rows;
second step query:
select * from table limit 5, 5
second step result:
last 3 rows;
But I want to get it vice versa. I mean from the first step I want last 3 rows and from the second I want 5 first rows. Thank you for your answer
No, you shouldn't do this. Without an ORDER BY clause you shouldn't rely on the order of the results being the same from query to query. It might work nicely during testing but the order is indeterminate and could break later. Use an order by.
SELECT * FROM table1 ORDER BY id LIMIT 5
By the way, another way of getting the last 3 rows is to reverse the order and select the first three rows:
SELECT * FROM table1 ORDER BY id DESC LIMIT 3
This will always work even if the number of rows in the result set isn't always 8.
Let's say we have a table with a column time and you want the last 5 entries, but you want them returned to you in asc order, not desc, this is how you do it:
select * from ( select * from `table` order by `time` desc limit 5 ) t order by `time` asc
yes, you can swap these 2 queries
select * from table limit 5, 5
select * from table limit 0, 5
This way is comparatively more easy
SELECT doc_id,serial_number,status FROM date_time ORDER BY date_time DESC LIMIT 0,1;
Related
I want to run a query where the last 5 entries are always returned, which is simple: SELECT * FROM table ORDER BY id DESC LIMIT 5. But I want to get more than 5 if a certain condition applies, which by itself would simply be something like SELECT * FROM table WHERE field > value. Is there a way to do this as a single query or do I have to run 2?
Maybe with UNION?. Like:
(SELECT * FROM table ORDER BY id DESC LIMIT 5)
UNION
(SELECT * FROM table WHERE field > value)
For instance, in table id/article/view_counter I would like to select top 5 articles by view_counter.
I could SELECT *, order by view count and then take only first 5 when I loop through array but is there a way to do that directly in query?
I believe you want to limit your records
SELECT ID, Article, count(view_counter) from table group by 1,2 order by 3 limit 5
Not sure your table structure, if view_Counter is already aggregate, you would just take the count and the group by off...
SELECT * from table order by 3 limit 5
This is assuming the view counter is your third column
If not then you would use
SELECT * from table order by view_counter limit 5
Lets say I have a 1000 rows in my table.
I want to select 10 of those at random.
SELECT * FROM table ORDER BY RAND() LIMIT 10
Then I want to select the row in that result with the highest value for number
SELECT * FROM table ORDER BY number DESC LIMIT 1
Can anyone help me come up with an efficient way of doing this?
Just use a subquery:
SELECT *
FROM (
SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1
How can I select specific rows in a MySQL Table? So instead of selecting ... ORDER BY date can I do something like SELECT * FROM Posts FROM(5) TO(10) ORDER BY date DESC where I would be selecting Posts 5 to 10 in the descending order?
Your question is a little ambiguous. There are two possibilities:
You want only part of the results to be retrieved from the database, for example from the 5th one to the 10th one:
SELECT * FROM `posts` ORDER BY `date` DESC LIMIT 6 OFFSET 4
which will skip the 4 first results and given you next 6 results (beginning with 5th from the original set and ending with 10th from the original set).
You want results with specific IDs between 5 (inclusive) and 10 (inclusive):
SELECT * FROM `posts` WHERE `id` BETWEEN 5 AND 10 ORDER BY `date` DESC
or (which can be useful if the list of IDs is not the whole range):
SELECT * FROM `posts` WHERE `id` IN (5,6,7,8,9,10) ORDER BY `date` DESC
but this last example is only to show you different way of picking "specific" rows.
use limit:
SELECT * FROM Posts ORDER BY date DESC LIMIT 5, 5
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
You can use limit operator in mysql to do this. Ex: SELECT * FROM posts LIMIT 5 , 5;
Alternative Syntax which is more readable in my opinion:
SELECT * FROM Posts ORDER BY date DESC LIMIT 5 OFFSET 5
The first number indicates the number of results returned, the second one defines the offset from where to begin with.
If you use the LIMIT 5,5 it's the other way round.
You can use the limit:
SELECT * FROM tabel ORDER BY date DESC LIMIT 5,5
(5th till 10th record based on descending date).
SELECT * FROM Posts ORDER BY date DESC limit 5, 10
i was wondering if there was an easy way with just an sql statement to return the last three results in the table but in that order i.e. if there are a hundered results it would return in the order of 98, 99, 100 not simply ordering by id DESC and limit 3 which would return in order 100, 99, 98
Any help much appreciated.
p.s. in this instance, lets say I don't know the amount of results and don't really want to send 2 sql requests just to find the amount ( for any OFFSET answers ).
One way would be to use DESC and then just sort them again:
SELECT * FROM (SELECT * FROM some_table ORDER BY id DESC LIMIT 3) a ORDER BY id
Two options, I guess.
You could use DESC to return them in reverse order as you stated, and just reverse the order again in your application code. This is potentially the most efficient, as you can do it in a single query and it can potentially only need to read three rows of an index.
You can first find out the number of results in the table, then do a LIMIT <results-3>, 3
Is it okay if you just flip it back to the original order?
SELECT * FROM (SELECT * FROM SOMETABLE ORDER BY ID DESC LIMIT 3) AS T ORDER BY ID;
Select *
FROM (SELECT * FROM yourTABLE ORDER BY ID DESC LIMIT 0,3) as TempTable ORDER BY ID