MySQL JOIN 和 COUNT 在单个查询中
我正在尝试将两个表连接在一起并获取外键的计数...... 抱歉,但我真的不知道如何解释自己,所以让我演示一下:
我有 1 个用于订单的表“orders”,具有以下字段:
id, f_name, l_name, credit_card, ETC.
然后,我有一个用于项目的“orders_details”表现在
id, order_id, product_id, qty
,我想运行一个连接 2 个表的查询,在订单表中每行获取 1 行,其中有一列告诉我每个订单中有多少产品。
有人知道如何实现这一目标吗?
PS 我还希望能够获得订单的所有“数量”总数(我不想为每个订单运行单独的查询)。
I'm trying to join 2 tables together and get the count of foreign keys...
I'm sorry, but I don't really how to explain myself, so let me demonstrate:
I have 1 table, 'orders', for orders, with the following fields:
id, f_name, l_name, credit_card, ETC.
Then, I have an 'orders_details' table for the items in the order, like so:
id, order_id, product_id, qty
Now, I want to run a query joining the 2 tables, getting 1 row per each row in the orders table, with a column telling me how many products are in each order.
Anybody know how to achieve this?
P.S. I'd also like to be able to get the total of all the 'qty' for the orders (I don't want to run a separate query for each order).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)