Php 选择计数最大值(值),其中

Php 选择计数最大值(值),其中,php,mysql,Php,Mysql,您好,您可以在附件的图片中看到,我想使用此查询提取数据 SELECT count(idmilestone) FROM milestoneevent WHERE max(idmilestone=1) 我想计算最后一个里程碑=1的所有文件,在我的情况下,它必须返回5,因为两个文件的里程碑现在是2,请帮助我实现这一点 试试这个 SELECT count(idmilestone) FROM milestoneevent WHERE idfile NOT IN(SELECT idfile

您好,您可以在附件的图片中看到,我想使用此查询提取数据

SELECT count(idmilestone) 
FROM  milestoneevent 
WHERE max(idmilestone=1)
我想计算最后一个里程碑=1的所有文件,在我的情况下,它必须返回5,因为两个文件的里程碑现在是2,请帮助我实现这一点

试试这个

SELECT count(idmilestone) 
FROM  milestoneevent 
WHERE idfile NOT IN(SELECT idfile 
    FROM milestoneevent 
    WHERE idfile > 1)
试试这个

SELECT count(idmilestone) 
FROM  milestoneevent 
WHERE idfile NOT IN(SELECT idfile 
    FROM milestoneevent 
    WHERE idfile > 1)

对于您提供的数据,里程碑的数量正在增加。如果这通常是正确的,您可以简单地执行以下操作:

select count(*)
from (select idfile, max(idmilestone) as max_idmilestone
      from milestoneevent
      group by idfile
     ) m
where max_idmilestone = 1;
或者,您可以使用
dat
定义最后一个:

select count(*)
from milestonemevent me
where me.dat = (select max(me2.dat)
                from milestonemevent me2
                where me.idfile = me2.idfile
               ) and
      me.idmilestone = 1;

对于您提供的数据,里程碑的数量正在增加。如果这通常是正确的,您可以简单地执行以下操作:

select count(*)
from (select idfile, max(idmilestone) as max_idmilestone
      from milestoneevent
      group by idfile
     ) m
where max_idmilestone = 1;
或者,您可以使用
dat
定义最后一个:

select count(*)
from milestonemevent me
where me.dat = (select max(me2.dat)
                from milestonemevent me2
                where me.idfile = me2.idfile
               ) and
      me.idmilestone = 1;

请尝试这个,它将给出idmilestone的计数,这是最小的一个,即1

SELECT count(idmilestone) FROM  milestoneevent 
WHERE idmilestone =(SELECT MIN(idmilestone) FROM milestoneevent)
如果你想要计数等于1,那么试试这个

SELECT count(idmilestone) FROM  milestoneevent 
WHERE idmilestone =1

请尝试这个,它将给出idmilestone的计数,这是最小的一个,即1

SELECT count(idmilestone) FROM  milestoneevent 
WHERE idmilestone =(SELECT MIN(idmilestone) FROM milestoneevent)
如果你想要计数等于1,那么试试这个

SELECT count(idmilestone) FROM  milestoneevent 
WHERE idmilestone =1

您可以为此使用
左连接

选择t1.id、t1.idfile、t1.idmilestone
从milestoneevent作为t1
左连接milestoneevent作为t2
在t1.idfile=t2.idfile和t1.dat

要计数,请使用
count

SELECT COUNT(*)
FROM milestoneevent AS t1
LEFT JOIN milestoneevent AS t2 
   ON t1.idfile = t2.idfile AND  t1.dat < t2.dat  
WHERE t1.idmilestone = 1 AND t2.id IS NULL
GROUP BY t1.idfile 
选择计数(*)
从milestoneevent作为t1
左连接milestoneevent作为t2
在t1.idfile=t2.idfile和t1.dat
您可以使用
左连接来执行以下操作:

选择t1.id、t1.idfile、t1.idmilestone
从milestoneevent作为t1
左连接milestoneevent作为t2
在t1.idfile=t2.idfile和t1.dat

要计数,请使用
count

SELECT COUNT(*)
FROM milestoneevent AS t1
LEFT JOIN milestoneevent AS t2 
   ON t1.idfile = t2.idfile AND  t1.dat < t2.dat  
WHERE t1.idmilestone = 1 AND t2.id IS NULL
GROUP BY t1.idfile 
选择计数(*)
从milestoneevent作为t1
左连接milestoneevent作为t2
在t1.idfile=t2.idfile和t1.dat
试试这个:

SELECT COUNT(*) 
FROM (
    SELECT idmilestone
    FROM milestoneevent 
    GROUP BY idmilestone
    HAVING COUNT(*) = 1
)
WHERE idmilestone=1
试试这个:

SELECT COUNT(*) 
FROM (
    SELECT idmilestone
    FROM milestoneevent 
    GROUP BY idmilestone
    HAVING COUNT(*) = 1
)
WHERE idmilestone=1


这是不正确的,此查询还将带来idfile 1,最后一个idmilestone是2不工作此返回7它,必须为正确的查询返回5您在问题中写道“我想计算最后一个milestone=1的所有文件”您想要什么它是如何来的5?它背后的逻辑是什么文件1和2有一个里程碑2,其他5个没有。这是不正确的,此查询还将带来idfile 1,最后一个idmilestone是2不工作此返回7它,必须返回5用于正确的查询您在问题中写道“我想计算最后一个里程碑=1的所有文件”您想要它是如何来的5?它背后的逻辑是什么?文件1和2有一个里程碑2,其他5个没有。为什么要使用max(idmilestone=1)有7个
idmileston=1
,为什么或如何得到5?这是因为有2个
idmileston=2的
idmileston=1
替换了
idmileston=1
。所以最后一个
idmilestone
是2,但是对于tow文件,最后一个里程碑id是2,所以我们不计算您有9条记录,7条记录的idmilestone=1。你需要澄清你的问题。你为什么要使用max(idmilestone=1)有7个
idmileston=1
,为什么或者如何得到5个?这是因为有2个
idfile
(1和2)取代了
idmileston=2
idmileston=1
。所以最后一个
idmilestone
是2,但是对于tow文件,最后一个里程碑id是2,所以我们不计算您有9条记录,7条记录的idmilestone=1。您需要澄清您的问题。谢谢,这是工作,但为什么这个别名必须是d?您可以在自己的自定义标签字段名称中得到结果。没有其他内容:)每个派生表都需要一个别名(即使没有使用),这很有用,但为什么需要这个别名呢?您可以在自定义标签字段名中获得结果。没有其他内容:)每个派生表都需要别名(即使未使用)