MYSQL查询按

时间:2016-06-27 00:37:40

标签: php mysql database

我有一个名为" message"的数据库表。我想获得每个用户的结果总消息以及来自它们的最后一条消息。下面是我的结构表和结果。这是我的SQL:

SELECT COUNT(email), messages.*
FROM messages
WHERE
email = message_id
group by email
order by created_at desc

这是我的表格结构

+---------------------+----------------+---------------------+---------------------+
| email               | message        | message_id          | created_at          |
+---------------------+----------------+---------------------+---------------------+
| memberONE@gmail.com | first message  | memberONE@gmail.com | 2016-06-26 10:00:00 |
+---------------------+----------------+---------------------+---------------------+
| Admin               | reply admin    | memberONE@gmail.com | 2016-06-26 12:00:00 |
+---------------------+----------------+---------------------+---------------------+
| memberONE@gmail.com | last message   | memberONE@gmail.com | 2016-06-26 22:00:00 |
+---------------------+----------------+---------------------+---------------------+
| memberTWO@gmail.com | first message  | memberTWO@gmail.com | 2016-06-26 10:00:00 |
+---------------------+----------------+---------------------+---------------------+
| memberTWO@gmail.com | second message | memberTWO@gmail.com | 2016-06-26 12:00:00 |
+---------------------+----------------+---------------------+---------------------+
| Admin               | reply admin    | memberTWO@gmail.com | 2016-06-26 18:00:00 |
+---------------------+----------------+---------------------+---------------------+
| memberTWO@gmail.com | last message   | memberTWO@gmail.com | 2016-06-26 22:00:00 |
+---------------------+----------------+---------------------+---------------------+

以上代码的结果是:

+--------------+---------------------+---------------+---------------------+---------------------+
| COUNT(email) | email               | message       | message_id          | created_at          |
+--------------+---------------------+---------------+---------------------+---------------------+
| 2            | memberONE@gmail.com | first message | memberONE@gmail.com | 2016-06-26 10:00:00 |
+--------------+---------------------+---------------+---------------------+---------------------+
| 3            | memberTWO@gmail.com | first message | memberTWO@gmail.com | 2016-06-26 10:00:00 |
+--------------+---------------------+---------------+---------------------+---------------------+

预期结果:只想从用户那里获取最后一条消息,其中 created_at 列是最后一个

+--------------+---------------------+--------------+---------------------+---------------------+
| COUNT(email) | email               | message      | message_id          | created_at          |
+--------------+---------------------+--------------+---------------------+---------------------+
| 2            | memberONE@gmail.com | last message | memberONE@gmail.com | 2016-06-26 22:00:00 |
+--------------+---------------------+--------------+---------------------+---------------------+
| 3            | memberTWO@gmail.com | last message | memberTWO@gmail.com | 2016-06-26 22:00:00 |
+--------------+---------------------+--------------+---------------------+---------------------+

请帮帮我。非常感谢你

2 个答案:

答案 0 :(得分:0)

SELECT * FROM (SELECT COUNT(email), messages.*
FROM messages
WHERE
email = message_id
order by created_at desc) x group by email;

如何运作的例子

select * from funding;
+------+--------+---------+
| area | client | Donatur |
+------+--------+---------+
| A    | Ox     | Mr.X    |
| A    | Pr     | Mr.Y    |
| A    | Qs     | Mr.Z    |
| A    | Ts     | Mr.Z    |
| B    | Rt     | Mr.X    |
| C    | Ss     | Mr.X    |
| C    | Sa     | Mr.Z    |
+------+--------+---------+

mysql> SELECT * FROM (select client,Donatur from funding  order by area DESC) X GROUP BY Donatur;
+--------+---------+
| client | Donatur |
+--------+---------+
| Ss     | Mr.X    |
| Pr     | Mr.Y    |
| Sa     | Mr.Z    |
+--------+---------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM (select client,Donatur from funding  order by area ) X GROUP BY Donatur;
+--------+---------+
| client | Donatur |
+--------+---------+
| Ox     | Mr.X    |
| Pr     | Mr.Y    |
| Qs     | Mr.Z    |
+--------+---------+
3 rows in set (0.00 sec)

答案 1 :(得分:0)

您可以使用join来执行此操作

mysql> select * from messages;
+----+----------------------+-----------------+----------------------+---------------------+
| id | email                | message         | message_id           | created_at          |
+----+----------------------+-----------------+----------------------+---------------------+
|  1 | memberONE#gmail.com  | first message   | memberONE#gmail.com  | 2016-06-26 10:00:00 |
|  2 | Admin                | replay admin    | memberONE#gmail.com  | 2016-06-26 12:00:00 |
|  3 | memberONE#gmail.com  | last message    | memberONE#gmail.com  | 2016-06-26 22:00:00 |
|  4 | memberTWO#gmail.com  | first message   | memberTWO#gmail.com  | 2016-06-26 10:00:00 |
|  5 | memberTWO#gmail.com  | second message  | memberTWO#gmail.com  | 2016-06-26 12:00:00 |
|  6 | Admin                | reply admin     | memberTWO#gmail.com  | 2016-06-26 18:00:00 |
|  7 | memberTWO#gmail.com  | last message    | memberTWO#gmail.com  | 2016-06-26 22:00:00 |
+----+----------------------+-----------------+----------------------+---------------------+
7 rows in set

mysql> SELECT
    m2.count,
    m1.*
FROM
    messages m1
INNER JOIN (
    SELECT
        count(email) AS count,
        email,
        max(created_at) AS created_at
    FROM
        messages
    WHERE
        email = message_id
    GROUP BY
        email
) m2 ON m1.created_at = m2.created_at
AND m1.email = m2.email;
+-------+----+---------------------+--------------+---------------------+---------------------+
| count | id | email               | message      | message_id          | created_at          |
+-------+----+---------------------+--------------+---------------------+---------------------+
|     2 |  3 | memberONE#gmail.com | last message | memberONE#gmail.com | 2016-06-26 22:00:00 |
|     3 |  7 | memberTWO#gmail.com | last message | memberTWO#gmail.com | 2016-06-26 22:00:00 |
+-------+----+---------------------+--------------+---------------------+---------------------+
2 rows in set