Jump to content

Join only the HIGHEST value from a table


mlat

Recommended Posts

Alright lets say I have two tables:

table_alpha
-----------
id


table_beta
-----------
alpha_id
time

Okay, the table beta table has multipule entries for each entry for table alpha. I need to get everything in table alpha, and then join one value from table_beta, which has the largest time value (im storing times in unix format). Anyone know a simple way of doing this?
Link to comment
Share on other sites

[quote author=mlat link=topic=103723.msg413229#msg413229 date=1155221702]
Alright lets say I have two tables:

table_alpha
-----------
id


table_beta
-----------
alpha_id
time

Okay, the table beta table has multipule entries for each entry for table alpha. I need to get everything in table alpha, and then join one value from table_beta, which has the largest time value (im storing times in unix format). Anyone know a simple way of doing this?
[/quote]

try something like this:

[code]
SELECT a.id, MAX(time) AS time FROM table_alpha a, table_beta b WHERE a.id = b.alpha_id GROUP BY a.id;
[/code]
Link to comment
Share on other sites

It's a rather nasty problem. I've posted this on another board and nobody there knows the solution either. I have a "backup solution" which would make the alpha table hold the second largest time, and just do things based upon that, but it requires a lot of recoding to sync this value. It'd be much better if I just got it working all in one query.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.