static IEnumerable<string> Combinations(List<string> characters, int length)
{
for (int i = 0; i < characters.Count; i++)
{
// only want 1 character, just return this one
if (length == 1)
yield return characters[i];
// want more than one character, return this one plus all combinations one shorter
// only use characters after the current one for the rest of the combinations
else
foreach (string next in Combinations(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1))
yield return characters[i] + next;
}
}
Вам необходимо изменить свой ORDER BY
:
SELECT * FROM table WHERE `id` > 1556 ORDER BY `id` ASC LIMIT 1
UNION
SELECT * FROM table WHERE `id` < 1556 ORDER BY `id` DESC LIMIT 1
Это гарантирует, что поле id
находится в правильном порядке, прежде чем брать лучший результат .
Вы также можете использовать MIN и MAX:
SELECT
*
FROM
table
WHERE
id = (SELECT MIN(id) FROM table where id > 1556)
OR id = (SELECT MAX(id) FROM table where id < 1556)
Следует отметить, что SELECT *
не рекомендуется использовать в производственном коде, поэтому назовите свои столбцы в своем SELECT
заявление.