gpt4 book ai didi

mysql - 具有多个表的 SQL 查询,可以仅对 count(*) 应用 group by 吗?

转载 作者:行者123 更新时间:2023-11-30 01:36:16 25 4
gpt4 key购买 nike

我正在尝试列出作业类型“N”的图书作业信息,并且出版商信用代码为“C”。然后,为先前查询输出的每一行添加 po 总数(采购订单 - 来自表 pos)。您可以使用 group by 仅应用于该计数而不应用于查询的其余部分吗?我必须使用联接吗?到目前为止我的尝试没有成功。这些是我正在使用的表:

bookjobs:
+--------+---------+----------+
| job_id | cust_id | jobtype |
+--------+---------+----------+
publishers:
+---------+------------+------------+
| cust_id | name | creditcode |
+---------+------------+------------+
pos:
+--------+-------+------------+-----------+
| job_id | po_id | po_date | vendor_id |
+--------+-------+------------+-----------+

这是我想出的,尽管它是错误的(计数未分组到 job_id):

select b.*, (select count(*) from pos o) as count 
from bookjobs b, publishers p, pos o
where b.cust_id=p.cust_id
and b.job_id=o.job_id
and b.jobtype='N'
and p.creditcode='C';

我相信我需要按 job_id 分组计数,而不是查询的其余部分。这是可能的还是我需要使用联接?我尝试了一些连接,但没有任何效果。任何帮助表示赞赏。

最佳答案

试试这个sql

select b.*, (select count(*) from pos where job_id=o.job_id) as count 
from bookjobs b, publishers p, pos o
where b.cust_id=p.cust_id
and b.job_id=o.job_id
and b.jobtype='N'
and p.creditcode='C';

关于mysql - 具有多个表的 SQL 查询,可以仅对 count(*) 应用 group by 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16834068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com