[英]SQL Subquery in VB.Net with Access Database
I have developed an SQL query that uses sub-query based on a result set (ie a query queries the results of a sub-query). 我开发了一个SQL查询,它使用基于结果集的子查询(即查询查询子查询的结果)。 The query executes as expected in MS Access but will not execute in vb.net (including query builder).
查询在MS Access中按预期执行,但不会在vb.net中执行(包括查询构建器)。 The error I receive in query builder is:
我在查询构建器中收到的错误是:
Error in WHERE clause near 'ORDER'.
“ORDER”附近的WHERE子句出错。 Unable to parse query text.
无法解析查询文本。
I understand that a typical sub-query selects based on the WHERE
clause whereas my SQL is querying based on the FROM
clause. 我知道典型的子查询基于
WHERE
子句选择,而我的SQL基于FROM
子句进行查询。 Because Access doesn't support the LIMIT
command, I can't figure out how structure this SQL so it will run in VB.NET. 因为Access不支持
LIMIT
命令,所以我无法弄清楚这个SQL的结构如何在VB.NET中运行。 If the LIMIT
command was supported I think I could structure the sub-query properly. 如果支持
LIMIT
命令,我想我可以正确地构造子查询。
Here is my SQL: 这是我的SQL:
SELECT AVG(Differential) * .96
FROM
(SELECT TOP 5 Differential
FROM
(SELECT TOP 10 GameDate, Differential
FROM ScoringHistory
WHERE PlayerID = ?
ORDER BY GameDate DESC)
ORDER BY Differential ASC, GameDate DESC)
If I change PlayerID = ?
如果我改变
PlayerID = ?
to PlayerID = 1
(which is a valid value), query builder gives me the same error as above but I can execute the query in query builder. 对于
PlayerID = 1
(这是一个有效值),查询构建器给出了与上面相同的错误,但我可以在查询构建器中执行查询。
I've also tried importing ( ?
) the query from Access into VB.NET and I get the same error. 我也尝试从Access导入(
?
)查询到VB.NET,我得到了同样的错误。
Any suggestions on how to structure my query so it will execute properly? 有关如何构建查询以便正确执行的任何建议?
Adding more information : 添加更多信息 :
I am implementing via a table adapter. 我通过表适配器实现。 See image.
见图。 Selecting the query and clicking configure displays the following
选择查询并单击“配置”将显示以下内容
Then if I click on the Query Builder button I get this error: Error Message 然后,如果我单击查询生成器按钮,我会收到此错误: 错误消息
My code to implement the query looks like this: 我实现查询的代码如下所示:
Dim factor As Single
factor = ScoringHistoryTableAdapter.CalculateFactor(1)
i think you are missing alis name or your subquery 我想你错过了alis名字或你的子查询
SELECT AVG(Differential) * .96
FROM
(SELECT TOP 5 Differential
FROM
(SELECT TOP 10 GameDate, Differential
FROM ScoringHistory
WHERE PlayerID = 1
ORDER BY GameDate DESC) a
ORDER BY Differential ASC, GameDate DESC) b
It looks like your OrderBy clauses are reversed. 看起来您的OrderBy子句是相反的。
You're ordering by GameDate in the middle From clause, but GameDate isn't included in that Select, only Differential. 你是GameDate在中间的From子句中订购的,但GameDate不包含在Select中,只有差异。
Try: 尝试:
SELECT AVG(Differential) * .96
FROM (
SELECT TOP 5 Differential
FROM (
SELECT TOP 10 GameDate, Differential
FROM ScoringHistory
WHERE PlayerID = ?
ORDER BY Differential ASC,GameDate DESC
)
ORDER BY Differential DESC
)
As the original poster I thought I would close the loop for everyone on this. 作为原始海报,我想我会为每个人关闭循环。
A little more research indicates that this is a known problem(?) with sub queries and the table adapter wizard. 还有一点研究表明,这是一个已知的问题(?),包含子查询和表适配器向导。 Now knowing this, i learned how to execute SQL the old fashioned way and used ExecuteScalar as Charles had indicated and the SQL works fine.
现在知道了这一点,我学会了如何以老式的方式执行SQL,并使用了Charles所指示的ExecuteScalar,并且SQL工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.