select next/previous 10 rows in mysql - mysql

I have a list that displays only 10 rows.
I need to select the next 10 and previous 10 rows.
However, the ID I use as reference are not exactly in order.
e.g:
1,2,5,10,15
How do I select the next few rows using the ID?

you can try limit:
select * from `table` limit <startIndex>,<NumberOfRecords>;
example:-
select * from `user` limit 5,10;
This will return 10 rows starting from 6th row.

a possible query would be
SELECT * FROM mytable WHERE id > current_id LIMIT 10
for the 10 previous
SELECT * FROM mytable WHERE id < current_id ORDER BY id DESC LIMIT 10

First select 10 first value:
SELECT * FROM `leave_type` ORDER BY id asc limit 10;
and then
select * from `leave_type` limit 10, 10;
will show rows after 10th value(range of 10) and start with 11th.

(
SELECT *
FROM mytable
WHERE id < $myid
ORDER BY
id DESC
LIMIT 10
)
UNION ALL
(
SELECT *
FROM mytable
WHERE id >= $myid
ORDER BY
id
LIMIT 10
)
ORDER BY
id

You can use limit keyword of MySQL.

Related

Reorder results from another query

SELECT * FROM (
SELECT * FROM cars WHERE site = '5'
ORDER BY cost DESC LIMIT 0 , 10
)
ORDER BY time
How would I execute a sql query like this? So first it selects the 10 cars with the highest cost, THEN it reorders those 10 cars by what time they were added to the DB.
I tried to figure it out but I just cannot get a grip on the syntax :P
Just give an alias to the sub-query.
SELECT * FROM (
SELECT * FROM `cars` WHERE `site` = '5'
ORDER BY `cost` DESC LIMIT 0 , 10
)t
ORDER BY `time`;
This query will give you the desired results
SELECT * FROM ( SELECT * FROM cars WHERE site = 5
ORDER BY cost DESC LIMIT 0 , 10 ) as t ORDER BY time

Count variable with limit in MySQL?

Nobody can answer this question?
$result=mysql_query("
SELECT COUNT(*) AS `total` FROM `mytable`
WHERE `myvariable`='1'
ORDER BY `id` DESC
LIMIT 15;"
);
$data=mysql_fetch_array($result);
$count = $data['total'];
echo $count;
This count ALL result from mytable, but how I can do to count last 15 results only? It seems LIMIT 15 not work in this case?
I think this is the query you want:
SELECT SUM(myvariable = '1') AS total
FROM (SELECT myvariable
FROM mytable
ORDER BY id DESC
LIMIT 15) AS subquery
This only looks at the most recent 15 rows, and counts the number of them that have myvariable = 1.
Since you want last 15 after descending order, Order by ascending and select first 15 and do descending order sort
select * from (SELECT * FROM mytable WHERE myvariable='1' ORDER BY id ASC LIMIT 15) ORDER BY id DESC

Is it possible to order "first n" records randomly with only one SELECT?

Is there a way to do this without two selects ?
Original query
SELECT name,view_count
FROM `ex`.`item`
where status='available' order by view_count asc
limit 40;
To display randomly
SELECT *
FROM (SELECT name,view_count
FROM `ex`.`item`
where status='available'
order by view_count asc
limit 40 ) AS temp
ORDER BY RAND();
Can this be done without a second select?
Try this:
SQL Fiddle: http://sqlfiddle.com/#!2/330f8/2
SELECT name, view_count
FROM `ex`.`item`
where status='available'
order by rand(), view_count asc limit 40;

Select last 3 rows of sql table

I want to select the last 3 rows of an sql table. I know I should use SELECT * FROM table ORDER BY DESC LIMIT 3, but the problem with this code is that it selects the rows from the end. For example, it selects 30, then 29, then 28. But, I need them in this format: 28, 29, 30. Any suggestion?
Try this:
SELECT * FROM (
SELECT * FROM reset ORDER BY id DESC LIMIT 3
) as r ORDER BY id
I hope this help your problem
select * from
(
select * from reset
order by id DESC LIMIT 3
) t
order by id ASC
Try something like this:-
SELECT * FROM reset
WHERE username = '$table' ORDER BY id ASC LIMIT (FOUND_ROWS() - 3), 3
How about something like:
select * from (select * from table order by x desc limit 3) order by x;
try
Select * from (SELECT * FROM Table_name ORDER BY Column_name DESC limit 0,3) as alias ORDER BY Column_name ASC;
try this manual one !
easy and Simple !!
Select * From tableName where
PKCol=(select count(*) from tableName )
OR
PKCol=(select count(*) from tableName )-1
OR
PKCol=(select count(*) from tableName )-2
order by PKCol desc;
It will help you out to give latest 3 rows data, if you wanna take first 3 rows then ASC instead of DESC.
select distinct column_name from Table order by column_name desc limit 3;

re-sorting the output of a SELECT LIMIT query

Fellow coders, i have a table that contains a number of rows each with a date column. I would like to select the last 6 most recent rows. I can do that like this:
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
This returns the rows I need but they are returned in DESC date order. What I want is the last 6 rows in ASC date order. How can I re-sort the output of the SELECT? Any ideas?
thanks
SELECT *
FROM (
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) s
ORDER BY s.StatsDate
Surround the query in an outer query and order that in a different order.
SELECT * FROM
(
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) s
ORDER BY `StatsDate` ASC
SELECT *
FROM (
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) as t
ORDER BY t.`StatsDate` ASC;