MS Access 查询问题

发布于 2024-11-30 02:49:34 字数 556 浏览 0 评论 0原文

我正在尝试计算有多少产品与我们的每个女性客户相关,以便我可以在以后的计算中使用它。

SELECT [Customers].Customer_ID
FROM [Customers]
WHERE ((([Customers].Gender)='Female'))
GROUP BY [Customers].Customer_ID;

上面的 SQL 为我提供了女性客户的 ID 列表。现在,我想在一个单独的查询中,我需要计算上面列表中每个 ID 的产品 ID 数量,但我不知道从哪里开始。我不知道是否可以将此步骤合并到上面的查询中,或者我是否需要做其他事情。

我想我需要这样的东西:

DCount("Order_ID", "Orders", "Customer_ID = [**how do I make it to where 
this equals the resulting Customer_ID's from the above query**]" )

有人可以帮忙吗?我走在正确的轨道上吗?

如果您需要更多信息,请告诉我。

I am trying to count how many products are associated with each of our female customers so I can use it in a later calculation.

SELECT [Customers].Customer_ID
FROM [Customers]
WHERE ((([Customers].Gender)='Female'))
GROUP BY [Customers].Customer_ID;

The above SQL gives me a list of ID's for our female customers. Now, I guess in a separate query, I need to count the number of Product ID's for each of the ID's in the above list, but I don't know where to go from here. I don't know if I can combine this step into the above query, or if I need to do something else.

I am thinking I need something like this:

DCount("Order_ID", "Orders", "Customer_ID = [**how do I make it to where 
this equals the resulting Customer_ID's from the above query**]" )

Can someone please help? Am I on the right track?

If you need more info, please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半步萧音过轻尘 2024-12-07 02:49:34

类似于:

SELECT [Customers].Customer_ID
     , COUNT(*) AS NumOrders
FROM [Customers]
  INNER JOIN [Orders]
    ON [Orders].Customer_ID = [Customers].Customer_ID
WHERE ([Customers].Gender = 'Female')
GROUP BY [Customers].Customer_ID;

INNER JOIN 应更改为 LEFT JOIN 以包含没有任何订单的客户(以及 COUNT(*) 应更改为 COUNT(Orders.Customer_ID) as @onedaywhen saw):

SELECT [Customers].Customer_ID
     , COUNT(Orders.Customer_ID) AS NumOrders
FROM [Customers]
  LEFT JOIN [Orders]
    ON [Orders].Customer_ID = [Customers].Customer_ID
WHERE ([Customers].Gender = 'Female')
GROUP BY [Customers].Customer_ID;

注意,上述查询将为您提供订单数量 而不是数量正如您所描述的产品。您必须提供表格的结构(可能是 OrdersOrderDetails 如果您有这样的表格),以便我们知道有关产品(及其订单)的信息在哪里) 被存储。



旁注:(再次,值得超过一半的分数:)@onedaywhen发现,Customer_ID应该是表[Customers]的PRIMARY KEY。如果是这种情况(或者如果它是UNIQUE),则您的原始查询可以简化:

SELECT [Customers].Customer_ID
FROM [Customers]
WHERE [Customers].Gender = 'Female' ;

Something like:

SELECT [Customers].Customer_ID
     , COUNT(*) AS NumOrders
FROM [Customers]
  INNER JOIN [Orders]
    ON [Orders].Customer_ID = [Customers].Customer_ID
WHERE ([Customers].Gender = 'Female')
GROUP BY [Customers].Customer_ID;

The INNER JOIN should be changed into LEFT JOIN to include Customers without any Orders (and COUNT(*) should be changed to COUNT(Orders.Customer_ID) as @onedaywhen spotted):

SELECT [Customers].Customer_ID
     , COUNT(Orders.Customer_ID) AS NumOrders
FROM [Customers]
  LEFT JOIN [Orders]
    ON [Orders].Customer_ID = [Customers].Customer_ID
WHERE ([Customers].Gender = 'Female')
GROUP BY [Customers].Customer_ID;

Note, that the above queries will give you number of Orders and not number of Products as you describe. You'll have to provide the structure of the tables (probably Orders, or OrderDetails if you have such a table) so we know where the information about products (and their orders) is stored.



Sidenote: as (again, deserving more than half of these points :) @onedaywhen spotted, the Customer_ID is supposed to be the PRIMARY KEY of table [Customers]. If that's the case (or if it is UNIQUE), your original query could be simplified:

SELECT [Customers].Customer_ID
FROM [Customers]
WHERE [Customers].Gender = 'Female' ;
箜明 2024-12-07 02:49:34

试试这个。

由于我在这台电脑上没有 MS Access,因此尚未对其进行测试。
类似的东西在 MySQL 和 MS SQL 中工作,所以它可能工作:)

SELECT SUM(O.Order_ID), C.Customer_ID
FROM [Orders] AS O, [Customers] AS C
WHERE C.Customer_ID IN (
SELECT [Customers].Customer_ID
FROM [Customers]
WHERE ((([Customers].Gender)='Female'))
GROUP BY [Customers].Customer_ID);

Try this.

Haven't tested it since I don't got MS Access on this PC.
Similar stuff work in MySQL and MS SQL so it might work :)

SELECT SUM(O.Order_ID), C.Customer_ID
FROM [Orders] AS O, [Customers] AS C
WHERE C.Customer_ID IN (
SELECT [Customers].Customer_ID
FROM [Customers]
WHERE ((([Customers].Gender)='Female'))
GROUP BY [Customers].Customer_ID);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文