3

I am trying to select the non repeated records of my database, and if there is one repeated, choose the one with the soonest date. To do so, I have tried the following query:

SELECT name, MIN(date) FROM `table` GROUP BY number_offer ORDER BY `date` ASC

and then, showing them like this:

echo $row['fecha'];
echo $row['name'];

The query selects correctly all the non repeated records but fails at selecting the sooner date (it says 'undefined index "date"'). Do you have any idea of how to do that?

Dharman
  • 30,962
  • 25
  • 85
  • 135
alberzyzz
  • 277
  • 2
  • 15

2 Answers2

3

the index fail is related to a missing alias

SELECT name, MIN(date) as `date`
FROM `table` 
GROUP BY number_offer 
ORDER BY `date` ASC

but you are select for name and grouping by number_offer ..the select for column not aggreation function and not in group by is deprecated
in sql starting recent db version

you should re evaluate you query using proper group by eg:

if you are select name you should group by name (and not number_offer)

SELECT name, MIN(date) 
FROM `table` 
GROUP BY name 
ORDER BY `date` ASC
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You can set MIN(date) as datee or MIN(date) datee

Dharman
  • 30,962
  • 25
  • 85
  • 135
kui.z
  • 74
  • 2