3

I would like to use some query that i found on this site - I adapted it for my own purposes:

SELECT * FROM 
(
(
SELECT id, lng FROM stations WHERE lng >= 18.123 ORDER BY lng LIMIT 1
) AS result1
UNION ALL
(
SELECT id, lng FROM stations WHERE lng < 18.123 ORDER BY lng LIMIT 1
) AS result2
)
ORDER BY abs(18.123-lng) LIMIT 1;

But I got an error Syntax error, unexpected AS, expecting UNION_SYM or ')'

When i try to do it without aliases, I (obviously) keep getting error Error Code: 1248. Every derived table must have its own alias

Could you please help me figure out what am I doing wrong? Thank you in advance.

Community
  • 1
  • 1
belialek
  • 79
  • 1
  • 1
  • 10

1 Answers1

7

You need to alias the outermost table, as such:

SELECT * FROM 
(
    (SELECT id, lng FROM stations WHERE lng >= 18.123 ORDER BY lng LIMIT 1)
    UNION ALL
    (SELECT id, lng FROM stations WHERE lng < 18.123 ORDER BY lng LIMIT 1)
) AS result12
ORDER BY abs(18.123-lng) LIMIT 1;

EDIT: forgot the parentheses around the inner queries.

VH-NZZ
  • 5,248
  • 4
  • 31
  • 47