我必须从两个表中选择数据,查询如下。
SELECT
accounts.firstname, accounts.mobileno, accounts.address, todotasks.lastmodified
FROM accounts
JOIN todotasks ON accounts.imeino = todotasks.assignedto
WHERE status = 'Completed'
现在我希望从表格中显示最新的修改时间。
答案 0 :(得分:0)
您可以使用ORDER BY todotasks.lastmodified DESC
-
SELECT
accounts.firstname, accounts.mobileno, accounts.address, todotasks.lastmodified
FROM accounts
JOIN todotasks ON accounts.imeino = todotasks.assignedto
WHERE status = 'Completed'
AND todotasks.lastmodified = (SELECT lastmodified FROM todotasks ORDER BY lastmodified DESC LIMIT 1)
答案 1 :(得分:0)
在我看来,您只想对每个帐户进行分组。鉴于我们无法使用您的完整表架构,以下是最佳猜测:
SELECT
accounts.firstname, accounts.mobileno, accounts.address, max(todotasks.lastmodified)
FROM accounts
JOIN todotasks ON accounts.imeino = todotasks.assignedto
WHERE status = 'Completed'
GROUP BY accounts.imeino
此处假设imeino
每个帐户都是唯一的,firstname
,mobileno
和address
不会因每个唯一的imeino而改变。
答案 2 :(得分:-1)
您只需group
您的数据,并使用MAX
函数选择最新日期:
SELECT a.firstname, a.mobileno, a.address, MAX(t.lastmodified) AS lastmodified
FROM accounts a
JOIN todotasks t ON a.imeino = t.assignedto
WHERE status = 'Completed'
GROUP BY a.firstname, a.mobileno, a.address
修改强>
SELECT a.firstname, a.mobileno, a.address, MAX(t1.lastmodified) AS lastmodified
FROM accounts a
JOIN todotasks t1 ON a.imeino = t1.assignedto
WHERE status = 'Completed' AND
NOT EXISTS(SELECT * FROM todotasks t2 WHERE t2.assignedto = t1.assignedto AND
t2.status <> 'Completed' AND
t2.lastmodified > t1.lastmodified
--AND t2.TaskNo = t1.TaskNo)
GROUP BY a.firstname, a.mobileno, a.address