从两个表中选择并删除ID匹配的位置

时间:2016-09-20 08:26:34

标签: mysql

我有两个表:job_applyjob_id, user_id, last_apply_date。如果用户应用作业,它将插入job_apply表。现在页面重新加载后,我想向用户显示除刚申请的作业之外的所有作业。我怎么能做到这一点?

这是job_apply表:job_id, user_id, job_title, job_description, job_wage, job_child_count, job_date, job_time_from, job_time_to, add_inserted

这是job_postings表SELECT tbl_users.user_id AS poster_id, userFirstName, userLastName, job_apply.job_id, job_postings.user_id, job_apply.user_id, job_title, job_description, job_wage, job_child_count, job_date, job_time_from, job_time_to, add_inserted, DATE_FORMAT(add_inserted,'%d %b %Y, at %T') AS add_inserted FROM job_postings JOIN tbl_users ON job_postings.user_id = tbl_users.user_id JOIN job_apply ON job_postings.job_id = job_apply.job_id WHERE job_apply.job_id = job_postings.job_id AND job_apply.user_id = 94 ORDER BY add_inserted DESC;

我试过了:

    SELECT tbl_users.user_id AS poster_id, userFirstName, userLastName, job_postings.job_id, job_postings.user_id, job_apply.user_id, job_title, job_description, job_wage, job_child_count, job_date, job_time_from, job_time_to, add_inserted,
  DATE_FORMAT(add_inserted,'%d %b %Y, at %T') AS add_inserted
  FROM job_postings
  JOIN tbl_users ON job_postings.user_id = tbl_users.user_id
  LEFT JOIN job_apply ON job_postings.job_id = job_apply.job_id
  AND job_apply.user_id = 83
  GROUP BY job_postings.job_id
  ORDER BY add_inserted DESC

修改

更新查询

SELECT tbl_users.user_id AS poster_id, userFirstName, userLastName, job_postings.job_id, job_postings.user_id, job_apply.user_id, job_title, job_description, job_wage, job_child_count, job_date, job_time_from, job_time_to, add_inserted,
  DATE_FORMAT(add_inserted,'%d %b %Y, kell %T') AS add_inserted
  FROM job_postings
    JOIN tbl_users ON job_postings.user_id = tbl_users.user_id

       JOIN (SELECT * FROM job_apply WHERE job_id IS NOT NULL) job_apply ON job_postings.job_id = job_apply.job_id

WHERE  job_apply.user_id = 94

编辑3

所以我用过这个:

 // Returns null
 "sssss.ssssssss.ssssss.sssss.ssss".match(/\w{17}/);

 // Returns ["sssssssssssssssss"]
 "ssssssssssssssssssssssssssss".match(/\w{17}/);

这显示了用户已应用的行,但我想获取用户未应用的值。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

添加AND job_apply.job_id IS NULL试试这个..

SELECT tbl_users.user_id AS poster_id, userFirstName, userLastName, job_apply.job_id, job_postings.user_id, job_apply.user_id, job_title, job_description, job_wage, job_child_count, job_date, job_time_from, job_time_to, add_inserted, 
DATE_FORMAT(add_inserted,'%d %b %Y, at %T') AS add_inserted 
FROM job_postings 
JOIN tbl_users ON job_postings.user_id = tbl_users.user_id 
LEFT JOIN job_apply ON job_postings.job_id = job_apply.job_id
WHERE job_apply.job_id = job_postings.job_id
AND job_apply.user_id = 94 AND job_apply.job_id IS NULL
ORDER BY add_inserted DESC;

您可以获得有关加入HERE ..

的更多详细信息

答案 1 :(得分:0)

这是最终的解决方案

SELECT tbl_users.user_id AS poster_id, userFirstName, userLastName, job_postings.job_id, job_postings.user_id, job_title, job_description, job_wage, job_child_count, job_date, job_time_from, job_time_to, add_inserted,
  DATE_FORMAT(add_inserted,'%d %b %Y, at %T') AS add_inserted
  FROM job_postings
  JOIN tbl_users ON job_postings.user_id = tbl_users.user_id

WHERE NOT EXISTS (
SELECT job_id FROM job_apply 
WHERE job_postings.job_id = job_apply.job_id AND job_apply.user_id = 116    
)