提问人:user1928432 提问时间:2/9/2023 最后编辑:Brian M Stafforduser1928432 更新时间:2/10/2023 访问量:119
在 VB6 中,如何查询访问记录,然后按特定 ID 排序
in vb6, how to query access records, then order by specific ids
问:
select * from [main] where id in (33,11,22) order by id
如果我使用此查询,我将通过以下方式获取记录集顺序:
11
22
33
但是我想按这个顺序返回记录集(与订单相同):id in (33,11,22)
33
11
22
通过谷歌,我了解我们可以使用它来实现:
select * from [main] where id in (33,11,22) order by charindex(ltrim(id),'33,11,22')
但问题是vb6不支持,我在使用时遇到了错误。charindex
The expression contains undefined function call CHARINDEX
那么我可以在 vb6 中做什么呢?谢谢。
更新:根据 @MarkL 的建议,我使用 to 而不是 ,查询正在运行但未按预期运行,返回具有以下顺序的记录集:instr
CHARINDEX
33
22
11
谢谢。
答:
4赞
Alex K.
2/10/2023
#1
select * from [main] where id in (33,11,22) order by InStr("33,11,22", id)
应该给你你想要的。
防止 ID 11 匹配“111,222”的更好版本是:
select * from [main] where id in (33,11,22) order by InStr(",33,11,22,", "," & id & ",")
基于集合的替代方案是拥有第二个“匹配”表,其中包含一个 AutoNumber RowNum 列和一个 Value 列,您可以在其中插入 33、11、22(按顺序),从而允许:
select * from [main] inner join matches on (matches.id = [main].id) order by rownum
评论
1赞
user1928432
2/10/2023
它有效!这帮助了我很多时间,几个小时在谷歌上找不到答案,非常感谢,最好的问候。
上一个:如何将逗号分隔的值拆分为列
评论
charindex
InStr