在 VB6 中,如何查询访问记录,然后按特定 ID 排序

in vb6, how to query access records, then order by specific ids

提问人:user1928432 提问时间:2/9/2023 最后编辑:Brian M Stafforduser1928432 更新时间:2/10/2023 访问量:119

问:

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不支持,我在使用时遇到了错误。charindexThe expression contains undefined function call CHARINDEX

那么我可以在 vb6 中做什么呢?谢谢。

更新:根据 @MarkL 的建议,我使用 to 而不是 ,查询正在运行但未按预期运行,返回具有以下顺序的记录集:instrCHARINDEX

33
22
11

谢谢。

SQL 数据库 ms-access-2007 charindex

评论

0赞 user1844933 2/9/2023
您正在使用哪个数据库?
0赞 user1844933 2/10/2023
此 QA 可能会解决您的问题 stackoverflow.com/a/15072297/1844933
0赞 user1928432 2/10/2023
@user1844933我使用 Access 2007,谢谢。
1赞 MarkL 2/10/2023
不是 vb6 问题。访问(可能更具体地说是 Jet 或 ODBC 驱动程序,无论您使用什么)不支持(这是 SQL Server 函数,也可能是其他数据库)。该函数可能有效 - 请参阅此 MS Access 文档charindexInStr

答:

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
它有效!这帮助了我很多时间,几个小时在谷歌上找不到答案,非常感谢,最好的问候。