2013-10-23 170 views
0

我有以下表相同的語句:JOIN沒有WHERE子句

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; 

但我想有他們在相同的結果集。我將如何實現這一目標?

回答

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)); 
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) 
0

這也將工作:

SELECT MAX(id) AS old_max, 
    (
     SELECT MAX(id) 
     FROM posts 
     ) AS max 
FROM posts 
WHERE TIME < DATE_SUB(NOW(), INTERVAL 1 HOUR); 
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)