MySQL查询和子查询不熟练

问题描述 投票:1回答:1

我有4张桌子:

  1. 访问 知识产权访谈 候选人FK候选人(#id候选人) 进程FK进程(#id_process) 结果
  2. 候选人 id_candidato PK
  3. Proceso id_proceso PK dpt FK DPT(#id_dpt)
  4. DPT id_dpt PK

我需要在我的查询中搜索每个'DPT',显示每个'proceso'的'id entrevista',但entrevista.resultado = 1并且必须是每个entrevista.candidato的最后一行

但如果我有更多行相同的entrevista.candidato:

因为我现在可以拿到entrevista.id entrevista的数量(数量):

SELECT p.id_proceso,
    (
            SELECT count(id_entrevista)
            from entrevista
            where entrevista.proceso = p.id_proceso
        ) as total
from proceso p
where dpt = 99   //this is the current dpt

但我需要子查询:

SELECT p.id_proceso,
        (
                SELECT count(id_entrevista)
                from entrevista
                where entrevista.proceso = p.id_proceso
            ) as total,
       (
        //here
         ) as approved
    from proceso p
    where dpt = 99   //this is the current dpt

我有这个想法,我需要检查entrevista中每个candidato的最后一个proceso。像这样的东西:

SELECT *
 FROM  entrevista
 WHERE proceso = 120 and candidato = 374
 ORDER BY fecha_entrevista DESC LIMIT 1

这将给我resultado,结果为1或0。我只需要count从查询中返回1的行

例如:

|entrevista |
 -----------
 0001 | 0001 | 0001 | 20-12-2017 | 1
 0002 | 0001 | 0001 | 21-12-2017 | 0
 0003 | 0002 | 0001 | 20-12-2017 | 1
 0004 | 0003 | 0001 | 20-12-2017 | 1

 | candidato |
 ----------
 0001 | Foo 
 0002 | Bar
 0003 | John Doe

 |proceso |
 ----------
 0001 | FooProceso | 0001

 | DPT |
 ----
 0001 | FooDPT

预期产量:

 Proceso    | Amount of interviews | Amount of pass
 --------------------------------------------------
 FooProceso | 4                    | 2
mysql subquery
1个回答
1
投票

谢谢你的帮助,最后我做到了:

SELECT p.id_proceso, p.proceso,
                               (
                                SELECT count(id_entrevista)                                                                 
                                from entrevista                                                                 
                                where entrevista.proceso = p.id_proceso                                                             
                               ) as q_entrevistas,                                                              
                             (
                                select count(distinct(ee.candidato))                                                                    
                                from entrevista ee                                                                  
                                where ee.proceso = p.id_proceso                                                                 
                                and ee.candidato not in (                                                                                                   
                                                          select e.candidato                                                                                                    
                                                          from entrevista e                                                                                                 
                                                          where e.proceso = p.id_proceso and e.resultado = 0
                                                        )
                            ) as aptos
FROM proceso p
WHERE p.dpt = $dpt //in this case i use PHP
© www.soinside.com 2019 - 2024. All rights reserved.