简体   繁体   中英

JOIN the same statement without WHERE clause

I have the following table:

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

And I want to know the highest ID in general, and I also want to know the latest post disregarding posts from the last hour so I can subtract them to get the number of posts from the past hour.

I could do two queries:

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

And

SELECT MAX(id) AS max FROM posts;

But I'd like to have them in the same result set. How would I achieve this?

How about this;

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

Try this::

Select 

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

(SELECT 
MAX(id) AS old_max 

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

This will also work:

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

this should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM