所以我有两张桌子:
如何进行联接以显示最新记录,即created_on DESC
例如
SELECT customers.name, files.last_file_submission
如果我有:
customer A
- File 1, created_on 10-10-2012
- File 2, created_on 11-10-2012
我希望为每个拥有以下内容的客户返回一行:
customer A, 11-10-2012
答案 0 :(得分:1)
每个文件:
SELECT c.name, f.submit_date
FROM files f, customers c
WHERE f.submitter = c.id
ORDER BY f.submit_date DESC
每位用户:
SELECT DISTINCT c.name, MAX(f.submit_date)
FROM files f, customers c
WHERE f.submitter = c.id
ORDER BY MAX(f.submit_date) DESC
GROUP BY c.id
答案 1 :(得分:0)
如果客户有任何文件记录,它将带来所有客户及其最新的文件日期。
SELECT DISTINCT customer.id, customer.name, MAX(file.submit_date)
FROM customer LEFT JOIN file ON customer.id = file.submitter
GROUP BY customer.id, customer.name
您可以使用LEFT
更改INNER
,以限制仅在具有文件表参考的客户之间加入。