Reorder results from another query - mysql

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

Related

How to take 2 avg() from mysql in phpmyadmin

SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='7' ORDER BY `counter` DESC LIMIT 75
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='4' ORDER BY `counter` DESC LIMIT 75
I want to see 2 avg easy to compare in my phpmyadmin. Singe line above will work but i can't combine them :/
Use conditional aggregation if you want the results in two columns:
SELECT AVG(CASE WHEN id = 7 THEN tmp END) as avg_7,
AVG(CASE WHEN id = 4 THEN tmp END) as avg_4
FROM monitor
WHERE id IN (4, 7);
The ORDER BY and LIMIT` are non-sensical in your question because the queries return only one row.
If you prefer two rows, then you have several choices. I think I would go with aggregation:
SELECT id, AVG(tmp) as average
FROM monitor
WHERE id IN (4, 7)
GROUP BY id;
Try:
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='7' ORDER BY `counter` DESC LIMIT 75
UNION
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='4' ORDER BY `counter` DESC LIMIT 75;

SELECT COUNT all rows and return just 10 + and a counted total

What I am trying to get is to send a request to DB that will:
1 count all rows
2 return 10 rows
SELECT count( * ) AS 'total'
FROM stuff
WHERE usr = '65'
LIMIT 10
So it is supposed to return 10 results PLUS 'total' with the number of all rows.
So far it returns the counted amount of rows only....
-- count records first
SET #total = (
SELECT count( * ) AS 'total'
FROM `stuff`
WHERE `usr` = '65'
);
-- then, select your ten records and include the total from previous operation
SELECT *, #total
FROM `stuff`
WHERE `usr` = '65'
LIMIT 10
You'll need to split the 2 concerns out, and then recombine them:
SELECT s.col1, s.col2, s.col3, x.total
FROM `stuff` s
CROSS JOIN
(
SELECT count(*) AS total
FROM `stuff`
WHERE `usr` = '65'
) x
WHERE s.`usr` = '65'
LIMIT 10;
Fiddle here
RDBMs like SqlServer and Oracle allow for CTE's which would allow you to DRY up the repeated select ... where. Some options in MySql here
If you want all rows to be counted and limit the result to 10 then you can do it the following way
SELECT SQL_CALC_FOUND_ROWS * FROM `stuff` WHERE `usr` = '65' LIMIT 10
SELECT FOUND_ROWS();

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;

MySql sort within sorted results set

I have the following query which queries a table of sports results for the last 20 matches that involved a teams, returning goals conceeded in each of these matches.
SELECT *, `against` AS `goalsF` , `for` AS `goalsA`
FROM `matches` , `teams` , `outcomes`
WHERE (
`home_team_id`=7 AND `matches`.away_team_id = `teams`.team_id
OR
`away_team_id`=7 AND `matches`.home_team_id = `teams`.team_id
)
AND `matches`.score_id = `outcomes`.outcome_id
ORDER BY `against', `date` DESC
LIMIT 0 , 20
I want sort the results by goals conceeded and then within each group of goals conceeded by date so for example.
the first 4 results where goals conceded=1 in date order
then the next 3 might be results where conceded=2 in date order
I have tried ORDER by date,against - this gives me a strict date order
I have tried ORDER by against,date - this gives me matches beyond the last 20
Is it possible to do what I want to do?
Thanks everyone, I found this worked. This solution was posted by another user but then was removed, not sure why?
SELECT * FROM (
SELECT *, `against` AS `goalsF` , `for` AS `goalsA`
FROM `matches` , `teams` , `outcomes`
WHERE (
`home_team_id`=7 AND `matches`.away_team_id = `teams`.team_id
OR
`away_team_id`=7 AND `matches`.home_team_id = `teams`.team_id
)
AND `matches`.score_id = `outcomes`.outcome_id
ORDER by `goalsF`
LIMIT 0 , 20
) res
ORDER BY `date` DESC
If you want to limit by date, add the date range you are looking for into your WHERE clause and then order by the number of goals conceded.

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;