我正在使用MySQL。我有两张桌子,我加入了。我需要使用count(a.parent_id)作为countcolumn,所以这个列将被添加到我用select创建的表中。我想知道每个" parent_id"出现但是根据下面的查询查看包含该列的所有表。该 问题是它只返回1条记录,并且它会像所有父级一样计算...
select count(parent_id) as count_column
, a.parent_id
, b.job_id
, a.status as parent
, b.status as subparent
, b.description
from jobs a
inner join jobs_steps b
on a.id=b.job_id
where name='XXX'
and a.status='FAILED'
order by a.parent_id desc
如果我在开头删除了计数父ID,则返回78条记录
select a.parent_id
, b.job_id
, a.status as parent
, b.status as subparent
, b.description
from jobs a
inner join jobs_steps b
on a.id=b.job_id
where name='XXX'
and a.status='FAILED'
order by a.parent_id desc
我希望有78条新记录的记录写出每个parent_id出现的数字。
答案 0 :(得分:1)
如果你想按照父ID分组你的结果,你应该使用select count(a.parent_id) as count_column
, a.parent_id
, b.job_id
, a.status as parent
, b.status as subparent
, b.description
from jobs a
inner join jobs_steps b
on a.id=b.job_id
where name='XXX'
and a.status='FAILED'
group by a.parent_id
order by a.parent_id desc
,但是这不会返回78条记录,因为这些记录不是每个父ID的组,这将返回一条记录每个父ID。我希望这有帮助
RewriteEngine On
RewriteRule ^uDoc/?$ display?type=doc [L,QSA,NC]
答案 1 :(得分:0)
假设您在每个作业中执行了多个步骤(作为每个作业详细信息的重复值)
通常我会使用窗口函数,但由于mySQL不支持它们,请使用查询来计算每个作业的parent_ID数量,然后加入...
select C.JobSteps as count_column
, a.parent_id
, b.job_id
, a.status as parent
, b.status as subparent
, b.description
from jobs a
inner join jobs_steps b
on a.id=b.job_id
LEFT JOIN (Select count(parent_ID) as JobSteps, job_ID
from Job_Steps
Group by Job_ID) C
on c.Job_ID = B.Job_ID
where name='XXX'
and a.status='FAILED'
order by a.parent_id desc
这对我来说似乎比在select中使用相关查询的替代方案更快。
select a.parent_id
, b.job_id
, a.status as parent
, b.status as subparent
, b.description
, (Select count(JS.parent_ID) as JobSteps, JS.job_ID
from Job_Steps JS
where a.Id = JS.Job_ID
Group by Job_ID) C as count_column
from jobs a
inner join jobs_steps b
on a.id=b.job_id
where name='XXX'
and a.status='FAILED'
order by a.parent_id desc