Jump to content

Recommended Posts

Sample data:

 

id1 id2 somecolumn
1    2        text
1    3        text
2    4        text
2    5        text
2    6        text

 

I want to subtract one row for each id1, and the row I want to select is for the highest value of id2. So for the above table, I want rows {1, 3, text} and {2, 6, text}.

 

id1 is non-unique, and will occur many times, but id2 will never occur more than once, even in the id1 table.

 

Can someone give me a hand with this? I've been pondering it for a while now, and haven't been able to come up with anything.

Link to comment
https://forums.phpfreaks.com/topic/173777-solved-query-about-a-query/
Share on other sites

Something like this:

 

mysql> select id1, max(id2), somecolumn from testing group by id1;
+------+----------+------------+
| id1  | max(id2) | somecolumn |
+------+----------+------------+
|    1 |        3 | text       | 
|    2 |        6 | text       | 
+------+----------+------------+
2 rows in set (0.00 sec)

 

Although I'm not sure what you mean by:

 

I want to subtract one row for each id1
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.