Select specific rows MySQL - mysql

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

Related

MySQL Get the first record in the last 10 records

in Mysql, I am trying to get the oldest record of the last 10 records.
To get the last 10 I would simply do
SELECT * FROM table ORDER BY id DESC LIMIT 10;
to get the oldest I simply use the ASC order.
I need to first order in DESC order to get the last 10 and then order by ASC to get the first record of that array.
which query would I use to get row number 10 in DESC order, namely, the oldest of the 10 newest?
Use [LIMIT {[offset,] row_count] concept:
SELECT * FROM table
ORDER BY id
DESC LIMIT 9, 1
Here, OFFSET is 9 --> that means 10th row (which will be first row in the last 10 rows when ordered back in Ascending order).
select only 1 using a subquery
SELECT * FROM ( SELECT * FROM ORDER BY id DESC LIMIT 10 ) order by id ASC LIMIT 1 ;

MySQL select last 'n' records by date, but sorted oldest to newest

I have a table that has transactions with a datetime column. I'm trying to select the last 'n' records (i.e. 20 rows) but have it sorted oldest to newest.
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20;
Gives me the 20 most recent, but in the opposite order.
Is this possible in one query, or will I have to do a query to get total rows and then adjust the limit based on that so I can do the table.datefiled ASC and then limit (total rows - n), n
Building a SELECT around your original SELECT and convert this to a derived table should do it
SELECT t.*
FROM (
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20
) t
ORDER BY t.datefield

How to select from a selection?

I'm creating a commenting system, which will have 2 top comments.
How can I select the latest 20 rows, and then from that selection, select the top 2 rows (likes-dislikes)? I can do it with a PHP loop, but it would not be as efficient. Currently I am just selecting the top 2 from the all the comments, but the two top comments never change, since people just up-vote those ones:
SELECT * FROM pagecomments WHERE page_id='$pageid' ORDER BY likes-dislikes DESC LIMIT 2
EDIT: The table is ordered by the the column "id", which is auto_increment. page_id is the page on the site. Sorry.
Nest your query:
SELECT *
FROM (
SELECT *
FROM pagecomments
WHERE page_id='$pageid'
ORDER BY date DESC
LIMIT 20
) t
ORDER BY likes-dislikes DESC
LIMIT 2
Order not only by LIkes, first order by date entered or timestamp. By ordering by date, you assure that you`ll get the latest 20 posts or comments.
SELECT * FROM pagecomments WHERE page_id='$pageid' ORDER by date_entered desc ,
likes-dislikes DESC limit 2
Since your id column is set to auto_increment, use it in a subquery:
select *, likes-dislikes
from (
select *
from pagecomments
where page_id='$pageid'
order by id desc
limit 20
) t
order by likes-dislikes desc
limit 2
Condensed SQL Fiddle Demo

why isn't this returning random stuff?

EDIT AGAIN: someone posted the solution, but it has a wierd x I don't understand why the x is there but now it works!! SOMEBODY EXPLAIN !!!
EDIT: I NEED THE LAST 12 RECORDS IN TIME, I USE ID INSTEAD OF TIME BECAUSE THE LATER ONES IN TIME ALSO HAVE A BIGGER ID, I GUESS THAT DOESN"T WORK BECAUSE OF THE PRIMARY KEY THING??
here is a mysql statement
"SELECT * FROM items WHERE item_section='$section'
ORDER BY item_id DESC, rand() LIMIT 12"
I need to add the DESC because I want the most recently posted item corresponding to a larger ID number. What I want is the last 12 records, but for those 12 to be in random order. This isn't working, what gives? I think I am just getting the latest 12 records NON-randomized.
If you turn what you said into sql, you get the answer:
SELECT * FROM (
SELECT * FROM items
WHERE item_section = '$section'
ORDER BY item_id DESC
LIMIT 12) x
ORDER BY rand()
You first select the most recent 12 records (aliased as x here), then you order them randomly... that takes two queries: one nested inside the other.
Because your item_id is probably a primary key and thus the order is fully determined and rand() is a noop.
EDIT: since you probably don't care about the answer "why", what you want to do is remove item_id DESC, part:
SELECT * FROM items WHERE item_section='$section'
ORDER BY rand() LIMIT 12
and if you want that sorted by id
SELECT * FROM (SELECT * FROM items WHERE item_section='$section'
ORDER BY rand() LIMIT 12) AS q ORDER BY item_id;
and if you want randomized first 12
SELECT * FROM (SELECT * FROM items WHERE item_section='$section'
ORDER BY item_id DESC LIMIT 12) AS q ORDER BY rand();

MySQL limit from descending order

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;