SELECT c1.category, c1.value FROM catvals c1 LEFT OUTER JOIN catvals c2 ON (c1.category = c2.category AND c1.value > c2.value) GROUP BY c1.category, c1.value HAVING COUNT(*) < 2;
Tested on MySQL 5.1.41 with your test data. Exit:
+----------+-------+ | category | value | +----------+-------+ | 1 | 1.30 | | 1 | 1.60 | | 2 | 9.20 | | 2 | 9.50 | | 3 | 4.00 | | 3 | 8.00 | +----------+-------+
(Additional decimal places are that I declared the value column as NUMERIC(9,2) .)
Like other solutions, this gives more than 2 lines for each category, if there are links. There are ways to create a join condition to solve it, but we will need to use a primary key or a unique key in your table, and we will also need to know how you intend to bind the relationships.
Bill karwin
source share