Jump to content

[SOLVED] Query about a query


haku

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.