У меня есть таблица: xyz
id |survey_id | submitted | username
---------------------------------------
155 | 8 | 1537276842 | 2
156 | 8 | 1537276842 | 2
157 | 8 | 1537276877 | 2
Я запускаю следующий запрос в базе данных:
SELECT DISTINCT case when result.aid < result.bid then result.aid else result.bid end as original, case when result.aid < result.bid then result.bid else result.aid end as duplicate FROM (SELECT a.id AS aid, b.id AS bid, a.submitted AS asubmitted, b.submitted AS bsubmitted FROM xyz AS a INNER JOIN xyz AS b ON (a.username = b.username AND a.survey_id = b.survey_id) WHERE a.id != b.id AND (abs(a.submitted - b.submitted) <= 300) ORDER BY a.id) AS result
И получаю:
original |duplicate
--------------------
155 | 157
155 | 156
156 | 157
После этого яхочу добавить серийный номер в первый столбец, затем я выполнил приведенный ниже запрос:
SELECT DISTINCT
@row_number:=@row_number+1 AS row_number,
case when result.aid < result.bid then result.aid
else result.bid end as original,
case when result.aid < result.bid then result.bid
else result.aid end as duplicate
FROM (SELECT a.id AS aid, b.id AS bid, a.submitted AS asubmitted, b.submitted AS bsubmitted
FROM xyz AS a
INNER JOIN xyz AS b ON (a.username = b.username AND a.survey_id = b.survey_id),
(SELECT @row_number:=0) AS result
WHERE a.id != b.id AND (abs(a.submitted - b.submitted) <= 300)
ORDER BY a.id) AS result
и get:
row_number |original |duplicate
---------------------------------------
1 | 155 | 157
2 | 155 | 156
3 | 156 | 157
4 | 155 | 156
5 | 155 | 157
6 | 156 | 157
Так что у меня есть проблема, почему он будетполучить двойной после добавления серийного номера к моему запросу.Мне нужен результат, как показано ниже в формате
row_number |original |duplicate
---------------------------------------
1 | 155 | 157
2 | 155 | 156
3 | 156 | 157