Answer the question
In order to leave comments, you need to log in
Can you help me form the correct MySQL query?
Good day.
I had a problem that killed a whole day of my life. But I never solved it. Of course, maybe for someone this is not a problem, but for me it still is.
There is a table of such structure:
bank_id | country | currency | buy | sell | dates
------------------------------------------------ ----------
1 | 1 | USD | 63.70 | 65.00 | 2016-08-26 23:34
2 | 4 | USD | 63.70 | 65.00 | 2016-08-26 22:33
1 | 5 | USD | 63.70 | 65.00 | 2016-08-26 23:33
3 | 1 | USD | 63.70 | 65.00 | 2016-08-26 21:13
2 | 3 | USD | 63.70 | 65.00 | 2016-08-26 23:23
1 | 4 | USD | 63.70 | 65.00 | 2016-08-26 23:16
2 | 5 | USD | 63.70 | 65.00 | 2016-08-26 22:15
1 | 2 | USD | 63.70 | 65.00 | 2016-08-26 23:21
3 | 4 | USD | 63.70 | 65.00 | 2016-08-26 22:11
2 | 1 | USD | 63.70 | 65.00 | 2016-08-26 19:33
How to create a query to display rows that match the conditions: country = 1, currency = USD, date = the latest, so that banks do not repeat, so that the current rate of the name in the city with id 1, in USD currency is displayed .
Answer the question
In order to leave comments, you need to log in
SELECT `v`.*
FROM (
SELECT `bank_id`, MAX(`date`) AS `date`
FROM `table`
WHERE `country` = :country_id AND `currency` = :currency_code
GROUP BY `bank_id`
) AS `d`
JOIN `table` AS `v`
ON `v`.`country` = :country_id AND `v`.`currency` = :currency_code
AND `v`.`bank_id` = `d`.`bank_id` AND `v`.`date` = `d`.`date`
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question