MySQL 查询和子查询专家

标签 mysql subquery

我有 4 个表:

  1. 入口
    • id_entrevista 公钥
    • 候选人 FK 候选人 (#id_candidato)
    • 过程 FK 过程 (#id_proceso)
    • 结果
  2. 候选人
    • id_candidato PK
  3. 进程
    • id_proceso PK
    • dpt FK DPT (#id_dpt)
  4. DPT
    • id_dpt 公钥

我需要在查询中搜索每个“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

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

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

最佳答案

谢谢你的帮助,最后我用:

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

关于MySQL 查询和子查询专家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47919693/

相关文章:

php - 奇怪的错误: Not on any other page with same code on lines

mysql - 现在()与 GetDate()

mysql - 添加两个子查询以生成第三列

mysql - SQL替代双重子查询

java - 使用 eclipselink 和 java.sql 的 JPA : when connect to DB

javascript - 如何使用AJAX、PHP和MySQL实时发出通知?

mysql - 将文本从一个表插入到另一个表中

php - laravel 访问子查询内的外部查询列

php - 在 mysql 查询中运行 mysql 查询

mysql - 根据 MySQL 中的条件更新不同的字段