任何建议都会很棒:
我正在运行MIN()
查询,其中我的帐号具有相同的状态和两个不同的时间戳
例如:
SELECT
DATE(`created_at`) as date,
account_id,
changed_by_id,
status
FROM statuses
WHERE `changed_to` = 'entered'
AND account_id = 49794;
结果:
date account_id changed_by_id status
2011-04-05 49794 17 entered
2011-03-16 49794 18 entered
当我尝试提取min(DATE(created_at))
和GROUP By account_id
时,我找不到changed_by_id
和account_id
date
任何人都可以建议我需要对查询做些什么才能将date
与changed_by_id
匹配?
由于
答案 0 :(得分:0)
未经测试,但它与此类似
select
t1.DATE(`created_at`) as date,
t1.account_id,
t1.changed_by_id,
t1.status
from statuses t1
where (date(created_at) = (select min(date(created_at))
from statuses t2
where t1.account_id=t2.account_id))
and t1.account_id = 49794;
答案 1 :(得分:0)
按日期排序,并仅选择第一行:
SELECT DATE(`created_at`) as date, account_id, changed_by_id, status FROM statuses WHERE `changed_to` = 'entered' AND account_id = 49794 ORDER BY 1 LIMIT 1;
这可能不是最优雅的选择,但它既清晰又有效。