在没有WHERE子句的情况下加入相同的语句

时间:2013-10-23 08:44:55

标签: mysql sql

我有下表:

CREATE TABLE posts...
id INT(11),
time DATETIME,
...

我想知道一般的最高ID,而且我也想知道最近的帖子忽略了过去一小时的帖子,所以我可以减去它们以获得过去一小时的帖子数量。

我可以做两个查询:

SELECT MAX(id) AS old_max FROM posts WHERE time < 
    DATE_SUB(NOW(), INTERVAL 1 HOUR);

SELECT MAX(id) AS max FROM posts;

但是我希望将它们放在同一个结果集中。我怎么做到这一点?

4 个答案:

答案 0 :(得分:0)

这个怎么样;

SELECT (
SELECT MAX(id) AS old_max FROM posts WHERE time < 
DATE_SUB(NOW(), INTERVAL 1 HOUR)),
(SELECT MAX(id) AS max FROM posts)

答案 1 :(得分:0)

试试这个::

Select 

(SELECT MAX(id) AS max from posts),

(SELECT 
MAX(id) AS old_max 

FROM posts WHERE time < 
    DATE_SUB(NOW(), INTERVAL 1 HOUR));

答案 2 :(得分:0)

这也有效:

SELECT MAX(id) AS old_max,
    (
        SELECT MAX(id) 
        FROM posts
        ) AS max
FROM posts
WHERE TIME < DATE_SUB(NOW(), INTERVAL 1 HOUR);

答案 3 :(得分:0)

这应该有效:

SELECT (
SELECT MAX(id) AS old_max FROM posts WHERE time < DATE_SUB(NOW(), INTERVAL 1 HOUR)),
(SELECT MAX(id) AS max FROM posts)