re-sorting the output of a SELECT LIMIT query - mysql

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;

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

How do i join 2 queries?

select a.nume_echipament,
a.producator,
a.seria,
b.uc,
b.port ,
a.durata_aprovizionare as durata_aprovizionare,
( SELECT sec_to_time(count(starea)*5) from echipamente inner join
aprovizionari on
echipamente.nume_echipament=aprovizionari.nume_echipament
where
(
echipamente.ora>aprovizionari.ora_aprov
and
echipamente.data>aprovizionari.data_aprov)
and starea='1'
and
echipamente.nume_echipament='automat_imbuteliere') as durata_functionare,
a.durata_viata as durata_viata,
( select data_aprov from aprovizionari where nume_echipament='automat_imbuteliere' order by date(data_aprov) desc, time(ora_aprov) desc limit 1) as data_aprov,
( select ora_aprov from aprovizionari where nume_echipament='automat_imbuteliere' order by date(data_aprov) desc, time(ora_aprov) desc limit 1) as ora_aprov,
(select sec_to_time(count(starea)*5)) as durata_totala
from date_tehnice a
inner join echipamente b on a.nume_echipament=b.nume_echipament
inner join aprovizionari c on c.nume_echipament=a.nume_echipament
where a.nume_echipament='automat_imbuteliere'
and
b.starea='1';
The above query work perfectly fine, but i also need to get the result from this query
select `starea` from echipamente where nume_echipament='automat_imbuteliere' order by data desc, ora desc limit 1 ;
As you can see in the first query it depends only on b.starea='1', whereas the second query needs to get the latest value of 'starea'. 'starea' has only 0 and 1 values. So, how do i combine this 2 queries into 1 query in order to get the last value value of 'starea'?
To get the second query to have its own row then UNION them like this with as many null columns as needed to match the number of columns in your first query.
UNION
select
(select `starea` from echipamente where nume_echipament='automat_imbuteliere' order by data desc, ora desc limit 1 )
, null
, null
, null
, null
, null

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

MySQL Sorting using order by not working using unio

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;

select next/previous 10 rows in 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.