MySQL选择5.0和5.5之间的查询差异

时间:2016-02-04 00:18:08

标签: mysql

以下查询正在MySQL 5.0.95和5.5.30上使用。

在5.5.30,它返回正确的结果(1个结果)。

在5.0.95上,它返回正确的结果但是有重复。所以它返回了两次(2个结果)。

我还没有在文档中看到任何会导致此问题的内容。

SELECT
  job.job_id, job.job_number, job.status, job_applicants.job_id, 
  job_applicants.user_id, job_applicants.accepted, 
  job_declined.job_id, job_declined.user_id, job_declined.declined
FROM job
    JOIN job_applicants ON job.job_id = job_applicants.job_id
    LEFT OUTER JOIN job_declined ON job.job_id = job_declined.job_id
WHERE 
    job_applicants.user_id = 6 AND job_applicants.accepted = 0 
    AND job.job_id NOT IN 
      (SELECT DISTINCT job_id FROM job_declined WHERE user_id = 6 and declined = 1)

1 个答案:

答案 0 :(得分:1)

这是job_declined的LEFT OUTER JOIN。这不需要在第一个查询中加入,只在子查询中加入。显然,5.0.95。

SELECT
  job.job_id, job.job_number, job.status, job_applicants.job_id, 
  job_applicants.user_id, job_applicants.accepted
FROM job
    JOIN job_applicants ON job.job_id = job_applicants.job_id
WHERE 
    job_applicants.user_id = 6 AND job_applicants.accepted = 0 
    AND job.job_id NOT IN 
      (SELECT DISTINCT job_id FROM job_declined WHERE user_id = 6 and declined = 1)