gpt4 book ai didi

sql - MySQL自定义排序

转载 作者:行者123 更新时间:2023-12-02 04:48:58 25 4
gpt4 key购买 nike

在编写 C# 程序时,我试图从 SQL 数据库中进行批量检索,而不是一次获取一条记录。这样我就得到了一个 DataSet 中的所有记录,并且只需访问服务器一次。我发现这种方法可以明显更快。但是我需要维持秩序。有没有人知道如何实现这一目标?

SELECT [UserID], [LastName], [FirstName]
FROM [users]
WHERE [UserID] = '2024443' OR [UserID] = '2205659' OR [UserID] = '2025493';

编辑:

我需要获取这样排序的数据:

202444322056592025493

最佳答案

要运行的适当查询是:

SELECT [UserID], [LastName], [FirstName]
FROM [users]
WHERE [UserID] IN ('2024443', '2205659', '2025493')
ORDER BY [UserID]

前提是您希望在 [UserID] 列的结果中保持顺序。

在澄清 OP 正在寻找的一个非常奇怪的订单后进行编辑:

因此预期的输出将按以下顺序包含选定的行:

'2024443''2205659''2025493'

So a simple ORDER BY that will order as a character field won't be enough. You should clarify what order it is because clearly you just want to sort based on those 3 rows only (eg: you haven't clarified where you would like to have number 2100000 and it is unpredictable).

For these kind of sorting you could go for an awful solution that will only work on those rows but as I said before, that's all you've provided. So you can do something like this:

SELECT [UserID], [LastName], [FirstName]
FROM [users]
WHERE [UserID] IN ('2024443', '2205659', '2025493')
ORDER BY
CASE [UserID]
WHEN '2024443' THEN 0
WHEN '2205659' THEN 1
ELSE 2
END

您应该能够使用该自定义排序构建其余查询。只需按照此作为模板即可。

关于sql - MySQL自定义排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19233746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com