mysql查询问题:获取最新帖子

时间:2014-02-08 00:02:59

标签: php mysql sql database

我有一张用户帖子的表格。

我试图通过此查询从帖子表中获取所有用户的最新帖子(仅1):

SELECT * FROM Posts WHERE PostedAs = 'USER' GROUP BY PostedAsId ORDER BY
CONCAT(PostedDate,PostedTime) DESC

每个用户只给我一个帖子,但问题是,它根据所有帖子给出了降序排列结果。

我想获取所有用户的最新帖子。 我做错了什么?

4 个答案:

答案 0 :(得分:1)

我认为这应该有效,但我不知道它是否足够快。

SELECT * 
FROM Posts p
WHERE PostedAs = 'USER' 
  AND CONCAT(PostedDate, PostedTime) = (SELECT MAX(CONCAT(PostedDate, PostedTime))
                                        FROM Posts p2
                                        WHERE p.PostedAsId = p2.PostedAsId)

答案 1 :(得分:0)

$sql = mysql_query("SELECT * FROM Posts WHERE PostedAs = 'USER' GROUP BY PostedAsId ORDER BY
CONCAT(PostedDate,PostedTime) DESC");
while($row = mysql_fetch_array($sql)
{
 echo $row['post'];
}

上述结果是什么?

答案 2 :(得分:0)

假设您有一个日期时间字段,我们称之为PostedTimestamp,您可以像这样进行查询:

SELECT * FROM Posts
INNER JOIN
    (SELECT
      PostedAsID,
      MAX(PostedTimestamp) AS latestPostTimestamp
    FROM Posts) AS LatestPosts
  ON Posts.PostedAsID = LatestPosts.PostedAsID
WHERE Posts.PostedAs = 'USER'

答案 3 :(得分:0)

我已经复制了你在初始问题中描述的内容,我无法看到原始concat查询的问题。

表格如下: -

mysql> desc posts;
+------------+------------+------+-----+------------+----------------+
| Field      | Type       | Null | Key | Default    | Extra          |
+------------+------------+------+-----+------------+----------------+
| PostId     | int(11)    | NO   | PRI | NULL       | auto_increment |
| PostedAs   | varchar(8) | YES  |     | USER       |                |
| PostedAsId | int(11)    | YES  |     | NULL       |                |
| PostedDate | date       | YES  |     | 0000-00-00 |                |
| PostedTime | time       | YES  |     | 00:00:00   |                |
+------------+------------+------+-----+------------+----------------+
5 rows in set (0.01 sec)

表中的数据为: -

mysql> select * from posts;
+--------+----------+------------+------------+------------+
| PostId | PostedAs | PostedAsId | PostedDate | PostedTime |
+--------+----------+------------+------------+------------+
|      1 | USER     |         22 | 1977-01-13 | 12:34:56   |
|      2 | USER     |         47 | 1977-02-13 | 00:00:00   |
|      3 | USER     |         52 | 1977-03-13 | 00:00:00   |
|      4 | USER     |          7 | 1977-04-13 | 00:00:00   |
|      5 | USER     |         47 | 1977-05-13 | 00:00:00   |
|      6 | USER     |         22 | 1977-06-13 | 00:00:00   |
|      7 | USER     |         52 | 1977-07-13 | 00:00:00   |
|      8 | USER     |         52 | 1977-08-13 | 00:00:00   |
|      9 | USER     |         59 | 1977-09-13 | 00:00:00   |
|     10 | USER     |          7 | 1977-10-13 | 00:00:00   |
|     11 | USER     |          7 | 1977-11-13 | 00:00:00   |
+--------+----------+------------+------------+------------+
11 rows in set (0.00 sec)

运行问题中提供的查询

 mysql> SELECT * FROM Posts
        -> WHERE PostedAs = 'USER'
        -> GROUP BY PostedAsId
        -> ORDER BY CONCAT(PostedDate,PostedTime) DESC;
    +--------+----------+------------+------------+------------+
    | PostId | PostedAs | PostedAsId | PostedDate | PostedTime |
    +--------+----------+------------+------------+------------+
    |      9 | USER     |         59 | 1977-09-13 | 00:00:00   |
    |      4 | USER     |          7 | 1977-04-13 | 00:00:00   |
    |      3 | USER     |         52 | 1977-03-13 | 00:00:00   |
    |      2 | USER     |         47 | 1977-02-13 | 00:00:00   |
    |      1 | USER     |         22 | 1977-01-13 | 12:34:56   |
    +--------+----------+------------+------------+------------+
    5 rows in set (0.00 sec)

重新安排相同的查询(排序): -

mysql> SELECT * FROM Posts
    -> WHERE PostedAs = 'USER'
    -> GROUP BY PostedAsId
    -> ORDER BY PostedDate DESC, PostedTime DESC;
+--------+----------+------------+------------+------------+
| PostId | PostedAs | PostedAsId | PostedDate | PostedTime |
+--------+----------+------------+------------+------------+
|      9 | USER     |         59 | 1977-09-13 | 00:00:00   |
|      4 | USER     |          7 | 1977-04-13 | 00:00:00   |
|      3 | USER     |         52 | 1977-03-13 | 00:00:00   |
|      2 | USER     |         47 | 1977-02-13 | 00:00:00   |
|      1 | USER     |         22 | 1977-01-13 | 12:34:56   |
+--------+----------+------------+------------+------------+
5 rows in set (0.00 sec)

使用您自己提供的concat order by,或者我自己的双重命令似乎为上面详述的表/数据提供正确的结果,每个用户被选中一次,post date / id是最新发布的一个

我可能会遗漏一些内容,因此请随意澄清您的架构是否与我在此处提供的架构有很大不同。

从表面上看,这个问题似乎在最初的问题上有了解决方案。

祝你好运!