SELECT a.rn1 AS ListNo, a.col1, a.col2, b.col1, b.col2
FROM (
SELECT a.*, @i := @i + 1 rn1
FROM yourTable a, (select @i := 0) lambda
ORDER BY Col2 DESC
) a
LEFT JOIN (
SELECT b.*, @j := @j + 1 rn2
FROM yourTable b, (select @j := 0) lambda
ORDER BY b.Col2 ASC
) b
ON a.rn1 = b.rn2 LIMIT 3
SELECT a.rn1 AS ListNo, a.col1, a.col2, b.col1, b.col2
FROM (
SELECT TOP 3 *, ROW_NUMBER() OVER (ORDER BY a.Col2 DESC) rn1
FROM yourTable a
ORDER BY Col2 DESC
) a
LEFT JOIN (
SELECT TOP 3 *, ROW_NUMBER() OVER (ORDER BY b.Col2 ASC) rn2
FROM yourTable b
ORDER BY Col2 ASC
) b
ON a.rn1 = b.rn2
หรือ
Code (SQL)
SELECT TOP 3 a.rn1 AS ListNo, a.col1, a.col2, b.col1, b.col2
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY a.Col2 DESC) rn1
FROM yourTable a
ORDER BY Col2 DESC
) a
LEFT JOIN (
SELECT *, ROW_NUMBER() OVER (ORDER BY b.Col2 ASC) rn2
FROM yourTable b
ORDER BY Col2 ASC
) b
ON a.rn1 = b.rn2
[x] โชคไม่ดีที่ MySQL ไม่มีฟังก์ชัน ROW_NUMBER()
นี่เป็นเหตุผลที่ผมไม่เคยหันมามอง MySQL