drcvvkx914772 2015-08-01 06:49
浏览 184
已采纳

加入两个MYSQL数据库表并检查第二个表是否没有记录然后返回FALSE,如果列有某个特定值,那么也是FALSE

I have two tables one for participants and one for shortlisted candidate. participants table have list of all applicants for a test, and shortlisted table has only shortlisted candidate. In the second table data gets inserted once a user click on shortlist button in UI (This part is correct). And when the user click the same button twice that candidate gets removed from shortlist (I have a fields "status" in shortlisted table and a flag value removed is set in it to un-shortlist the candidate)

Now when i try to pull all candidates using the following query i am not getting the desired output.

My requirement is to pull all candidates who are not shortlisted.

P.S If a student has not been selected once, there won't be any entry for that student in shortlisted table. If a student has been selected once and the removed, there will be an entry with his detail in shortlisted table with status removed

My current query is as follows

    SELECT DISTINCT(gca.leaderEmail)lemail,
gca.leaderId,
gca.leaderName, 
gca.leaderMobile, 
gca.clgName, 
gca.t1Name, 
gca.t2Name, 
gca.id caseApplyId, 
gca.caseId caseId, 
gu.profile_pic,
        CASE WHEN gcs.id IS NULL then FALSE else TRUE end as shortlist
        FROM gmmu_case_apply gca
        LEFT JOIN gmmu_users gu ON gu.id = gca.leaderId AND gca.leaderId <> 0
        LEFT JOIN gmmu_case_shortlist gcs ON gcs.applicantId = gca.id
        WHERE gca.caseId = $caseId

Can you help me to figure out what wrong i am doing here to achieve my desired output

Update: My requirement is I should get FALSE if in second table that candidate has a status = 'removed' and also FALSE in case there is no matching JOIN for that user in Second table. I want the return of the alias shortlist either to be TRUE or FALSE I have reached some distance with this line in the query above

CASE WHEN gcs.id IS NULL then FALSE else TRUE end as shortlist
  • 写回答

2条回答 默认 最新

  • dongyao2129 2015-08-01 07:02
    关注

    I believe you need to change your join on gmmu_case_shortlist to an LEFT JOIN as well. The query as you have it written is only going to return records that are in both gca and gcs.

    Also, your case statement needs to be modified to check gcs.status for 'removed' like:

    CASE WHEN gcs.status='removed' THEN FALSE 
         WHEN gcs.id is null THEN FALSE 
         else TRUE 
    end as shortlist
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。