我正在使用MySQL。我有两张桌子,我正在加入。我需要使用count(a.parent_id)作为countcolumn,所以这个列将被添加到我用select创建的表中。我想知道每个“parent_id”出现的数字,但要根据下面的查询查看包含该列的所有表。该 问题是,它仅返回1个记录,类似有同样的它计算所有父..MYSQL与计数或有计数问题
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
如果我删除它返回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
开始计数父ID我希望有78个新记录栏记录每个parent_id出现的编号。
即使mySQL不需要它,(由于[扩展组] by(https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html)子句)在使用诸如count,max,avg,min之类的聚合函数时,所有不属于聚合的部分的列应包含在“group by”子句中。否则,引擎可以自由选择未聚合列中的单个值并返回其值。如果所有的值都是相同的,但是如果值不同,那么定义组可能会产生更接近所需的结果。这解释了为什么你只获得1条记录。 – xQbert
我想你想'GROUP BY a.parent_id ,b.job_id ,a.status父 ,b.status为subparent ,b.description'不仅仅是a.parent_ID – xQbert