我尝试从3表中查看显示DISTINCT记录的查询。但是也显示重复用户记录。
SELECT DISTINCT users.sid, users.username, users.registration_date, users.FirstName,
users.LastName, users.phoneNumber, listings.Resume, uploaded_files.saved_file_name
FROM users JOIN listings
ON users.sid = listings.user_sid
JOIN uploaded_files
ON listings.Resume=uploaded_files.id
WHERE listings.listing_type_sid = '7' AND listings.Resume != 'NULL'
答案 0 :(得分:1)
对表的结构做出一些大的假设。
明显的方法是加入一个获取每个用户最新上市日期的子查询,然后将其与列表相关联以获取该日期的列表字段。
SELECT users.sid,
users.username,
users.registration_date,
users.FirstName,
users.LastName,
users.phoneNumber,
listings.Resume,
uploaded_files.saved_file_name
FROM users
INNER JOIN
(
SELECT user_sid, MAX(resume_date) AS latest_resume
FROM listings
GROUP BY user_sid
) sub0
ON users.sid = sub0.user_sid
INNER JOIN listings
ON sub0.sid = listings.user_sid
AND sub0.latest_resume = listings.resume_date
INNER JOIN uploaded_files
ON listings.Resume=uploaded_files.id
WHERE listings.listing_type_sid = '7'
AND listings.Resume != 'NULL'
有点小提琴是使用GROUP_CONCAT获取按日期排序的所有已保存文件,然后使用SUBSTRING_INDEX获取第一个(我刚刚使用默认逗号分割文件 - 但你应该真的使用分隔的,永远不会出现在任何文件名中)
SELECT users.sid,
users.username,
users.registration_date,
users.FirstName,
users.LastName,
users.phoneNumber,
listings.Resume,
SUBSTRING_INDEX(GROUP_CONCAT(uploaded_files.saved_file_name ORDER BY listings.resume_date DESC), ',', 1) AS saved_file_name
FROM users
INNER JOIN listings
ON users.sid = listings.user_sid
INNER JOIN uploaded_files
ON listings.Resume=uploaded_files.id
WHERE listings.listing_type_sid = '7'
AND listings.Resume != 'NULL'
GROUP BY users.sid,
users.username,
users.registration_date,
users.FirstName,
users.LastName,
users.phoneNumber,
listings.Resume