使用 SQL 进行条件选择

Ying

我有表申请人有这样一个领域:id, crew_id, name我还有一张桌子

`certificates: id, applicant_id, certificate number`.
'documents: id, applicant_id, document_number, expired.
'tests: id, applicant_id, name, score.

我想要的只是返回申请人状态,所以如果证书中有申请者ID,状态将是“招聘中”,如果证书中有申请者ID,文件状态将是“正在处理”,如果证书文件测试表中有申请者ID 则状态将“完成”。是否可以仅通过使用 select 和 sub select 来完成此操作?

所以后者的结果将是这样的记录:申请人 ID:10500,状态:'完成'。也许像 IF(从证书中选择申请人_id,其中 c.applicant_id = a.id)作为状态?

去世

您可以将表applicant与其他 3 个表保持连接,并针对您的条件使用 CASE 语句:

select a.*,
  case 
    when c.applicant_id is not null and d.applicant_id is not null and c.applicant_id is not null then 'complete'
    when c.applicant_id is not null and d.applicant_id is not null then 'processing'
    when c.applicant_id is not null then 'under recruitment'
    else null
  end result
from applicant a
left join certificates c on c.applicant_id = a.id
left join documents d on d.applicant_id = a.id
left join tests t on t.applicant_id = a.id

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章