L
L
lightalex2016-06-15 18:31:28
PHP
lightalex, 2016-06-15 18:31:28

How to fetch from a MySQL database?

Hello colleagues!
There is an entity with attributes name and date
Using php, we send a mysql query to the selection
. We need to get from all names one by one with the largest date .
Example
Let's say we have the following table:

Name | Date
-----------------
Петя | 10.02.2014
Петя | 21.05.2013
Ваня | 21.05.2013
Петя | 30.07.2015
Ваня | 03.10.2012
Даня | 12.08.2014

We get the following lines:
Петя | 30.07.2015
Ваня | 21.05.2013
Даня | 12.08.2014

That is, the names are not repeated, but among the same names the line with the largest date is selected
. Is it possible to implement this with one request?
PS The dates in the database are in the correct format.
This is just for illustration purposes .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
safenoob, 2016-06-15
@safenoob

SELECT DISTINCT `name`, `date` FROM `table` ORDER BY `date`

A
Andrey, 2016-06-15
@VladimirAndreev

select
max(`Date`), `Name`
from `Tbl`
group by `Name`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question