The problem is that I want to obtain two adjacent rows with a row of records in a table and want to obtain them through union, so I write:
The problem is that I want to obtain two adjacent rows with a row of records in a table and want to obtain them through union, so I write:
The problem is that I want to obtain two adjacent rows with a row of records in a table and want to obtain them through union, so I write:
Select id, title from subjects where id> # some_id # order by id limit 1
Union
Select id, title from subjects where id <# some_id # order by id limit 1
However, the error message "Incorrect usage of UNION and order by" appears ". It seems that union and order by cannot be used, but order by is indeed required here. Soon I came up with a work und:
Select * from (
Select id, title from subjects where id> # some_id # order by id limit 1
) Union
Select id, title from subjects where id <# some_id # order by id limit 1
In terms of experience, the second clause will not be affected by union. order by can be used. So we can wrap the first clause in a bracket. But there is still an error, prompting "Every derived table must have its own alias ". The prompt here is that we need to get an alias for the temporary table generated in the brackets, which is much easier. So it is changed:
Select * from (
Select id, title from subjects where id> # some_id # order by id limit 1
) As t1 union
Select id, title from subjects where id <# some_id # order by id limit 1