我希望通过一封distinct
电子邮件获取一些数据。
我的代码如下:
SELECT DISTINCT Email,
Degree,
Majority
FROM job_education a
WHERE a.Status = 'Active'
AND a.IdEducationLevel = (
SELECT Max(b.IdEducationLevel)
FROM job_education b
WHERE b.Status='Active'
AND a.Email = b.Email
)
感谢您的善意
答案 0 :(得分:1)
你应该使用连接
(子选择不能看到表a的上部引用)
SELECT DISTINCT Email,
Degree,
Majority
FROM job_education a
INNER JOIN (
SELECT b.Email, Max(b.IdEducationLevel) max_IdEducationLevel
FROM job_education b
WHERE b.Status='Active'
GROUP BY b.Email
) t ON a.Email = t.Email and a.IdEducationLevel = t.maxy_IdEducationLevel
WHERE a.Status = 'Active'
答案 1 :(得分:1)
你的代码应该没问题,虽然我会修复表别名:
SELECT je.*
FROM job_education je
WHERE je.Status = 'Active' AND
je.IdEducationLevel = (SELECT MAX(je2.IdEducationLevel)
FROM job_education je2
WHERE je2.Status = 'Active' AND je2.Email = je.Email
);
如果您为给定的电子邮件获取多行 - 并且您只想要一行 - 那么在子查询中使用更好的ID:
SELECT je.*
FROM job_education je
WHERE je.Status = 'Active' AND
je.job_education_id = (SELECT je2.job_education_id
FROM job_education je2
WHERE je2.Status = 'Active' AND je2.Email = je.Email
ORDER BY IdEducationLevel DESC, job_education_id DESC
LIMIT 1
);
为此,我发明了一个名为job_education_id
的id。请注意,此查询使用ORDER BY
和LIMIT
而不是聚合函数。