Forum Moderators: open
Table users
¦id ¦ name .¦Age¦
+---+-------+---+
¦ 1 ¦ steve ¦12 ¦
¦ 4 ¦ Johny ¦51 ¦
Table users scores
¦id¦ score ¦ level ¦ date .¦
-----------------------------
¦1 ¦ 1000 ¦level 1 ¦12/10/06 ¦
¦4 ¦ 4550 ¦level 5 ¦12/10/06 ¦
¦1 ¦ 1450 ¦level 8 ¦18/10/06 ¦
¦1 ¦ 5660 ¦level 7 ¦24/10/06 ¦
What I need to do is pull a list of all the user from table user and the last score posted by each user from the user_scores.
The user score database also includes other information that needs to be pulled out.
Tried to keep my explanation simple hope someone can help
With that in mind (and don't forget I'm not a wiz here) I see two options, one with 1 query, one with two different queries (multiple times):
1 query:
SELECT * FROM both tables ASCENDING
In a for loop push() every id into an array, and the later dates will overwrite the earlier ones because of the ASCENDING result.
2 queries:
SELECT all unique ids
in a for loop SELECT * from both tables where the id's match, DESC LIMIT 1
That will get you the last entry for each id.
I'm interested to here how this can be done in pure sql.
SELECT users.id, users.name, users.age, scores.score, scores.level, MAX(scores.`date.`) AS `date` FROM `users`, `scores` WHERE users.id=scores.id GROUP BY scores.id
As I tested it with the table you provided, it returned correct results.
Regards
Michal
PS. Glad to be of help, pixeltierra
but,
due to my stupidity, when I've uploaded it to my server it broke. My server runs mysql 4.1.11.
I never thought about it being a different mysql version, its never been a problem before, but then I've never done complex mysql before.
I gone throught the mysql manuel, but I can't find any reason why it has broken.