From: Larry Martell
Date: September 23 2013 7:35pm
Subject: Problem with having
List-Archive: http://lists.mysql.com/mysql/229841
Message-Id:
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=047d7ba97f3ab9f61904e712202a
--047d7ba97f3ab9f61904e712202a
Content-Type: text/plain; charset=UTF-8
I want to find the rows from a table that have the max date_time for each
recipe. I know I've done this before with group by and having, but I can't
seem to get it to work now. I get the correct row id, but not the correct
date_time. I'm sure I'm missing something simple.
For purposes of showing an example, I'll use one recipe, 19166.
For that recipe here's the row I would want:
mysql> select id, MAX(date_time) from data_cstmeta where recipe_id = 19166;
+---------+---------------------+
| id | MAX(date_time) |
+---------+---------------------+
| 1151701 | 2013-02-07 18:38:13 |
+---------+---------------------+
1 row in set (0.01 sec)
I would think this query would give me that - it gives me the correct id,
but not the correct date_time:
mysql> SELECT id, date_time as MaxDateTime FROM data_cstmeta where
recipe_id = 19166 group by recipe_id HAVING MIN(date_time);
+---------+---------------------+
| id | MaxDateTime |
+---------+---------------------+
| 1151701 | 2010-12-13 16:16:55 |
+---------+---------------------+
1 row in set (0.01 sec)
How can I fix this?
Thanks!
-larry
--047d7ba97f3ab9f61904e712202a--