mysql select n rows before and n rows after - php

I am trying to create a mysql query to select let's say 5 rows before and 5 rows depending on the currend ID. Here is the query I am trying to use:
SELECT * FROM posts WHERE PID<='10' ORDER BY PID DESC LIMIT 7
UNION ALL
SELECT * FROM posts WHERE PID>'10' ORDER BY PID ASC LIMIT 6
For some reason though, the query is not working. Am I missing something? I found this solution in a forum post.

Try this:
(SELECT *
FROM posts
WHERE PID <= '10'
ORDER BY PID DESC LIMIT 7
)
UNION ALL
(SELECT *
FROM posts
WHERE PID > '10'
ORDER BY PID ASC LIMIT 6
)
ORDER BY PID ASC;
sqlfiddle demo

The PID is likely of int type. So you should be using the following (SQL Fiddle):
(SELECT * FROM posts WHERE PID <= 10 ORDER BY PID DESC LIMIT 7)
UNION ALL
(SELECT * FROM posts WHERE PID > 10 ORDER BY PID ASC LIMIT 6)
As you can see the ordering of the two queries is also maintained separately.

DOCS
To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:

Related

How to get ORDERED and limited rows form MySQL and randomize show order

Is possible, and how to ask MySQL for
SELECT * FROM my_table ORDER by row_id DESC LIMIT 8
get the last 8, newest record from my table, with randomized order for PHP showing method
$results = $mysqli->query($query);
while($row = $results->fetch_assoc()) {
echo $row['my_col_name'];
}
Colud I, and where put the rand() in my SQL query?
Without randomize I get last 8 rows ORDERED 10,9,8,7,6,5,4,3
I want to get in the following order:
9,7,5,4,6,10,3,8;
8,7,3,6,10,9,5,4
...
You can place it inside another select:
SELECT * FROM (SELECT * FROM my_table ORDER by row_id DESC LIMIT 8) t ORDER BY RAND()
Use a subquery:
SELECT t.*
FROM (SELECT t.*
FROM my_table t
ORDER by row_id DESC
LIMIT 8
) t
ORDER BY rand();

Select last 5 mysql rows

i want to get the last 6 rows but skip the last one in result:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 6
How can i make this?
Try this:
SELECT *
FROM (SELECT *
FROM TableName
ORDER BY FieldName DESC
LIMIT 5) sub
ORDER BY FieldName ASC
Try this:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 1,6

Select on Mysql inverse order

i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order

Mysql Multiple Subquery Limit

I have a table with the following columns:
playlistid
trackname
playstatus
likes
dislikes
created
playstatus can have the following values:0, 1, 2 and 4.
The sql query that Im using now is:
SELECT * from playlist
WHERE playlistid=$myplaylistid && playstatus=0
ORDER BY (likes-dislikes) DESC, created ASC
Its worth to mention that there at all times only will be one row with playstatus = 1, one row with playlistid=3, but there will be multiple rows with playstatus = 0 and 2.
I want to change the query above so that I get 1 row from where playstatus=3, 1 row from where playstatus=1, and 3 rows from where playstatus=0 in one single query. All these have to be chosen based on the same orderby that you see in the query above.
All help is appreciated! Thank you for your time!
Try this query
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=1
ORDER BY (likes-dislikes) DESC, created ASC limit 1
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=3
ORDER BY (likes-dislikes) DESC, created ASC limit 1
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=0
ORDER BY (likes-dislikes) DESC, created ASC limit 3
Try this
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=3
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=1
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=0
ORDER BY (likes-dislikes) DESC, created ASC

return random 5 records from last 20 records

Simply I have the following
table with records , i want to return randomly 5 records from the last 20 record (order by id desc)
so how we can do it fast
thanks for help.
select * from
(
select * from your_table
order by id desc limit 20
) as lastest_results
order by rand()
limit 5;
Use an inner query to return the last 20, and an outer query to select 5 of them randomly. This may be slow though.
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 20) t ORDER BY RAND() LIMIT 5;
This is a slow method but it gets the JOB done:
ORDER BY RAND()
LIMIT 5;
If you are using a large table this can become very slow,
There are multiple alternative that you can read about here
Alternative to order by rand
You need something like
SELECT * FROM table ORDER BY RAND() LIMIT 5;
If you have a time parameter to sort them by for example the last 20 records then use this.
SELECT * FROM table ORDER BY insert_time DESC, RAND() LIMIT 5;
OR
SELECT * FROM table ORDER BY id DESC, RAND() LIMIT 5;

Categories