[英]How to select max value of each group when this value is stored in timestamp type column?
I have the following table in my database.我的数据库中有下表。
CREATE TABLE `market_messages` (
`pm_id` int(11) NOT NULL,
`pm_item_index` int(11) DEFAULT NULL,
`pm_item_id` varchar(11) COLLATE utf8_polish_ci NOT NULL,
`pm_item_sender` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_item_client` varchar(30) COLLATE utf8_polish_ci NOT NULL,
`pm_item_sender_id` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_item_client_id` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_item_name` varchar(80) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_sender` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_receiver` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_entry_start` timestamp NULL DEFAULT NULL,
`pm_message` text COLLATE utf8_polish_ci DEFAULT NULL,
`pm_index` int(11) DEFAULT NULL,
`pm_type` varchar(10) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_user_offer` varchar(50) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_status` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;
Now I'm trying to group messages and order in specific sequence on the list.现在我正在尝试对列表中的消息进行分组和排序。 I need to list comments based on last entry into
m_entry_start
column which type is timestamp.我需要根据类型为时间戳的
m_entry_start
列的最后一个条目列出评论。
Here is my query, but result is not good:这是我的查询,但结果不好:
$sql = "SELECT *
FROM market_messages
WHERE (pm_item_client = '$user_name'
AND pm_item_client_id = '$user_id')
OR (pm_item_sender = '$user_name'
AND pm_item_sender_id = '$user_id')
GROUP BY pm_item_id, pm_item_client_id, pm_item_sender_id
ORDER BY MAX(pm_entry_start) DESC;";
Result is not this what I have expected.结果不是我所期望的。
Echo $fetch['m_entry_start'];
doesn't give me right result.没有给我正确的结果。
I'm expecting max value of pm_entry_start
timestamp from each group, but in results is taken probably first value.我期待每个组的
pm_entry_start
时间戳的最大值,但在结果中可能采用第一个值。 Additionally `pm_entry_start' is echoed with ago() function to show elapsed time.此外,`pm_entry_start' 与 ago() 函数相呼应以显示经过的时间。 Ago function is converting timestamp in format 2021-10-01 10:00:19 to something like 3 minutes ago, or 1 day ago etc. This is example result:
Ago 函数正在将 2021-10-01 10:00:19 格式的时间戳转换为 3 分钟前或 1 天前等格式。这是示例结果:
$user_name ![]() |
ago(timestamp)![]() |
---|---|
Adam![]() |
1 month ago ![]() |
Eve![]() |
23 days ago ![]() |
Terese![]() |
2 months ago ![]() |
Mark![]() |
5 days ago ![]() |
Monica![]() |
3 min ago ![]() |
I expecting results in order based on timestamp in descending order so from the last entry to the first.我期望根据时间戳按降序排列结果,因此从最后一个条目到第一个条目。
$user_name ![]() |
ago(timestamp)![]() |
---|---|
Monica![]() |
3 min ago ![]() |
Mark![]() |
5 days ago ![]() |
Eve![]() |
23 days ago ![]() |
Adam![]() |
1 month ago ![]() |
Terese![]() |
2 months ago ![]() |
How to write the good query?如何编写好的查询? I have started programming in PHP last year.
我去年开始用 PHP 编程。 This project is my first with use of MySQL database.
这个项目是我第一次使用 MySQL 数据库。 Any help appreciated.
任何帮助表示赞赏。
Finally I have found the solution: SELECT ..., MAX(pm_entry_start) AS pm_entry_start FROM ...
was missing part of MySQL query.最后我找到了解决方案:
SELECT ..., MAX(pm_entry_start) AS pm_entry_start FROM ...
缺少 MySQL 查询的一部分。 Before I have tried only: SELECT ..., MAX(pm_entry_start) FROM ...
之前我只尝试过:
SELECT ..., MAX(pm_entry_start) FROM ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.