Created
March 1, 2012 16:51
-
-
Save kenmazaika/1951328 to your computer and use it in GitHub Desktop.
Tyler's Max 'B' For Every 'A' Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here's an example -- this returns the max "b" record for every "a". | |
Tyler | |
mysql> create table x (a int, b int); | |
Query OK, 0 rows affected (0.18 sec) | |
mysql> insert x values (1, 1), (1, 2), (1, 3), (2, 5), (2, 8), (3, 10); | |
Query OK, 6 rows affected (0.04 sec) | |
Records: 6 Duplicates: 0 Warnings: 0 | |
mysql> select * from x x1 left join x x2 on x1.a = x2.a and x2.b > x1.b where x2.a is null; | |
+------+------+------+------+ | |
| a | b | a | b | | |
+------+------+------+------+ | |
| 1 | 3 | NULL | NULL | | |
| 2 | 8 | NULL | NULL | | |
| 3 | 10 | NULL | NULL | | |
+------+------+------+------+ | |
3 rows in set (0.00 sec) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment