MS Access,如何将参数传递给子查询?
我有两张桌子。一个包含我的所有客户,另一个包含他们的所有订单。 我想创建一个如下所示的 sql 查询:
SELECT c.CustomerID, c.Firstname, c.Lastname,
(
SELECT count(orderID)
FROM tbl_orders o
WHERE o.CustomerID = c.CustomerID
) as OrderCount
FROM tbl_customers c;
我遇到的问题是访问不断要求我提供将在子选择 WHERE 子句中使用的参数“CustomerID”。显然,我想自动弄清楚。我该怎么做?
i've got two tables. one contains all my customers, another one all their orders.
i would like to create an sql query that looks like this:
SELECT c.CustomerID, c.Firstname, c.Lastname,
(
SELECT count(orderID)
FROM tbl_orders o
WHERE o.CustomerID = c.CustomerID
) as OrderCount
FROM tbl_customers c;
The problem I have with this is that access keeps asking me for the parameter "CustomerID" which would be used in the subselect WHERE clause. Obviously, I'd like to figure it out automatically. How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有明显的原因,它应该有效。也许您在某个字段上拼写错误。检查 CustomerID 是否是 tbl_orders & 的一部分tbl_customers。
您可以通过以下链接找到有关子查询的一些信息:SubQueryDoc
There is no apparent reason, it should work. Maybe you made a mispell on one of your fields. Check if CustomerID is part of tbl_orders & tbl_customers.
You can find some information about sub query thru the following link : SubQueryDoc
为什么不尝试使用 join 和 group by 呢?对我来说,它似乎更简单、更丰富。
可以在查询设计视图中直观地设计这样的查询。然后,您可以切换到 SQL 视图,并复制或编辑生成的 SQL 语句。
Why not trying with a join and a group by ? it seems simpler and richer to me.
Such a query can be designed visually in the query design view. You can then switch to SQL View, and copy or edit the generated SQL statement.
删除主查询和子查询上的别名“c”。
Remove alias "c" on both main and sub query.