2012-03-26 5 views
0

ich in mysql folgenden Tabelle habenWie kann ich Untergruppe meine mysql Ergebnisse

+-----------------+-------------+------+-----+---------+----------------+ 
| Field   | Type  | Null | Key | Default | Extra   | 
+-----------------+-------------+------+-----+---------+----------------+ 
| id    | int(11)  | NO | PRI | NULL | auto_increment | 
| user_profile_id | int(11)  | NO | MUL | NULL |    | 
| latitude  | float(10,6) | YES |  | NULL |    | 
| longitude  | float(10,6) | NO |  | NULL |    | 
| modified  | datetime | YES |  | NULL |    | 
| created   | datetime | YES |  | NULL |    | 
+-----------------+-------------+------+-----+---------+----------------+ 

Ich möchte eine Abfrage tun, wo ich eine Liste von user_profile_id s passieren und ich ihre letzte Platte zurück. Jede user_profile_id hat mehrere Datensätze

sql : select id,user_profile_id,latitude,longitude,modified,created from user_locations where user_profile_id in(1044,1259); 

    +----+-----------------+-----------+-------------+---------------------+-------------------- -+ 
| id | user_profile_id | latitude | longitude | modified   | created    | 
+----+-----------------+-----------+-------------+---------------------+---------------------+ 
| 14 |   1044 | 49.276867 | -123.120689 | 2011-12-24 00:50:22 | 2011-09-06 19:09:18 | 
| 59 |   1044 | 49.276867 | -123.120689 | 2011-08-05 12:12:12 | 2011-11-01 00:00:00 | 
| 60 |   1044 | 49.276867 | -123.120689 | 2010-08-05 12:12:12 | 2010-11-01 00:00:00 | 
| 61 |   1044 | 49.276867 | -123.120689 | 2009-08-05 12:12:12 | 2009-11-01 00:00:00 | 
| 62 |   1044 | 49.276867 | -123.120689 | 2008-08-05 12:12:12 | 2008-11-01 00:00:00 | 
| 41 |   1259 | 49.276722 | -123.120735 | 2011-12-08 19:53:39 | 2011-12-07 19:38:02 | 
+----+-----------------+-----------+-------------+---------------------+---------------------+ 

Beispiel:

select *,max(created) from user_locations where user_profile_id IN (1044,1259) group by user_profile_id; 

Diese Abfrage ist falsch, wie kann dieses Problem beheben? Danke

Antwort

1
select u.id,u.user_profile_id,u.latitude,u.longitude,u.modified,u.created 
from user_locations u where u.user_profile_id in(1044,1259) and u.created = 
(select max(g.created) from user_locations g where u.user_profile_id=g.user_profile_id) group by u.user_profile_id; 
+0

es gibt alle Ergebnisse, funktionierte nicht, thx obwohl – apoo

+0

hast du in (1044,1259) mit dem entsprechenden IDs ersetzen? auch welche Sprache verwenden Sie, um Daten an mysql übergeben – kasavbere

+0

1044,1259 sind geeignete IDs, ich schreibe direkt in mysql Client, möchte sicherstellen, dass die SQL-Anweisung zuerst korrekt ist. Ihr Vorschlag gibt alle Ergebnisse für user_profile_id = 1044 zurück – apoo