我有一个以下模式的表:
+----+--------+----------------------------+----------------------------+
| id | amount | created_timestamp | updated_timestamp |
+----+--------+----------------------------+----------------------------+
| 1 | 1.00 | 2018-01-09 12:42:38.973222 | 2018-01-09 12:42:38.973222 |
+----+--------+----------------------------+----------------------------+
在这里,对于id = 1
,可能有多个金额条目。我想提取最后添加的条目及其对应的数量,按 id 分组。
我在 self 表上编写了一个带有内部连接的工作查询,如下所示:
SELECT t1.id,
t1.amount,
t1.created_timestamp,
t1.updated_timestamp
FROM transactions AS t1
INNER JOIN (SELECT id,
Max(updated_timestamp) AS last_transaction_time
FROM transactions
GROUP BY id) AS latest_transactions
ON latest_transactions.id = t1.id
AND latest_transactions.last_transaction_time =
t1.updated_timestamp;
我认为内部联接是一种矫枉过正的做法,可以用更优化/更高效的查询来代替。我用 , 编写了以下查询,where
但它不起作用。任何人都可以帮忙吗?group by
having
select id, any_value(`updated_timestamp`), any_value(amount) from transactions group by `id` having max(`updated_timestamp`);