Answer the question
In order to leave comments, you need to log in
How to compose the following SQL query?
The initial selection is sorted in ascending order by ID_1 and ID_2.
ID_2 is always greater than ID_1.
It is necessary to obtain such a selection, where the ID_2 of one record will be less than the ID_1 of the next record.
That is, for the above selection, the result will be the following:
How to create such a query and is it possible (on PostgreSql or MySql)?
Answer the question
In order to leave comments, you need to log in
For a primitive case, we always select the first available entry:
SET @last = 0;
SELECT `ID_1`, @last := `ID_2` AS `ID_2`
FROM (
SELECT `ID_1`, `ID_2`
FROM `table`
ORDER BY `ID_1`, `ID_2`
) AS `t`
WHERE `ID_1` > @last;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question