Mysql filter SELECT query result. - mysql

I have the following query.
SELECT COUNT(id) AS NumResults, race_date AS RaceDate, race_time AS RaceTime
FROM results
WHere jockeys_claim = 10
GROUP BY race_date DESC, race_time ASC
I would then like to filter out the NumResults , so the table only shows results where the NumResults = 1. Thanks for looking.

Use the HAVING clause to filter the results of an aggregate function (the COUNT in your case)
SELECT COUNT(id) AS NumResults, race_date AS RaceDate, race_time AS RaceTime
FROM results
WHere jockeys_claim = 10
GROUP BY race_date, race_time
HAVING COUNT(id) = 1
ORDER BY race_date DESC, race_time 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

SQL Multi Order by Count with Criteria

I want to query standings from MotoGP Race result
my sql is
SELECT `Rider`, `Team`, `Bike`,SUM(`Points`)
FROM `table_name`
WHERE `Year` = 2015 AND `Classes` = "MotoGP"
GROUP BY `Year`,`Rider`
HAVING SUM(`Points`)
ORDER BY SUM(`Points`) DESC
If 2 or more riders have same SUM('Points'), the next order is by number of 1st race position.
I put the race position on 'Pos' field.
Pos value: 1,2,3,4,5 ...
Please help for the 2nd order. Thank you
You just need to change the HAVING clause:
ORDER BY SUM(`Points`) DESC,
SUM(RacePosition = 1) DESC
For multiple positions, I think you need to add the condition for each one:
ORDER BY SUM(`Points`) DESC,
SUM(RacePosition = 1) DESC,
SUM(RacePosition = 2) DESC,
SUM(RacePosition = 3) DESC
At some point, perhaps SUM(RacePosition) would do what you want.

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 get latest records by order by desc without subquery

Im trying to get result with below query but its taking around 2min to retrieve
SELECT *
FROM customer e
WHERE e.id=324
AND e.g_id IN('x133fv','be6544','e992170','93611c')
and e.enrol_id =
(
select e1.enrol_id
from customer e1
WHERE e1.id=324
AND e1.g_id=e.g_id
ORDER BY update_time DESC, posted_time DESC, enrol_id DESC
LIMIT 1
)
I have index on (g_id,id)
Is there any other way to get the result via JOIN?
This is your query:
SELECT *
FROM customer e
WHERE e.id = 324 AND
e.g_id IN ('x133fv','be6544','e992170','93611c') and
e.enrol_id = (select e1.enrol_id
from customer e1
WHERE e1.id=324 AND e1.g_id=e.g_id
ORDER BY update_time DESC, posted_time DESC, enrol_id DESC
LIMIT 1
)
You can improve the performance with indexes. I would suggest: customer(id, g_id, update_time, posted_time, enrol_id).

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;