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;
Related
I want to combine these two queries.
SELECT * FROM table1 WHERE status='pending' and adr='' order by id desc limit 0,1;
SELECT * FROM table1 where status='pending' and adr='new' ORDER BY RAND() LIMIT 1
You can use a UNION ALL set operator to concatenate the results of the two queries
( SELECT * FROM table1 WHERE status='pending' AND adr='' ORDER BY id DESC LIMIT 1 )
UNION ALL
( SELECT * FROM table1 WHERE status='pending' AND adr='new' ORDER BY RAND() LIMIT 1 )
Reference: UNION ALL https://dev.mysql.com/doc/refman/5.5/en/union.html
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;
I'm using an union statement in mysql but i've some problems sorting the results. The ORDER statement doesn't works at all, the results comes out always sorted by the id field.
Here an example query:
SELECT a.* FROM ( ( select * from ticket_ticket AS t1 WHERE ticket_active=1 ORDER BY t1.ticket_date_last_modified DESC )
UNION ( select * from ticket_ticket AS t2 WHERE ticket_active=0 ORDER BY t2.ticket_date_last_modified DESC, t2.ticket_status_id DESC ) )
AS a LIMIT 0,20;
I want to order the results of the first SELECT by last_modified time, and the second SELECT by time and status. But the ORDER statement get just skipped. The results always come out ordered by the ticket_id ( the PRIMARY KEY ).
What's wrong in this query ?
Thanks!
Ok, i've fixed it writing the query this way:
SELECT a.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=1
ORDER BY ticket_date_last_modified DESC) AS a
UNION ALL
SELECT b.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=0
ORDER BY ticket_date_last_modified DESC, ticket_status_id DESC) AS b LIMIT 0,
20;
You are using a UNION query that will return distinct values, and the order of the returned rows is not guaranteed.
But you don't need an union query for this:
select *
from ticket_ticket AS t1
ORDER BY
ticket_active!=1,
ticket_date_last_modified DESC,
ticket_status_id DESC
LIMIT 0,20;
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;
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.