2017-08-01 46 views
0

我正在嘗試獲取我網站上最後一個活動聊天用戶的數據集,但查詢中的where子句是「失敗」並且返回結果已經「過期」 ......MySQL查詢使用Date_Sub返回查詢中的空集,但包含連接時count = 1

以下是我所期望的......

mysql> select * from openchats where date_sub(now(), interval 15 minute) < active; 
Empty set (0.00 sec) 

不過,我加入一個情侶對錶,使我的生活更輕鬆,並且連接查詢當我期望爲零時返回計數1 ...

mysql> SELECT 
    -> (
    ->  COUNT(*) 
    -> ) AS `count` 
    -> FROM 
    -> openchats Openchats 
    -> LEFT JOIN users Users ON Users.id = (Openchats.user_id) 
    -> LEFT JOIN chatrooms Chatrooms ON Chatrooms.id = (Openchats.chatroom_id) 
    -> WHERE 
    -> (
    ->  DATE_SUB(NOW(), interval 15 minute) < 'Openchats.active' 
    ->  AND open = 1 
    -> ); 
+-------+ 
| count | 
+-------+ 
|  1 | 
+-------+ 
1 row in set, 1 warning (0.00 sec) 

下面是表中的數據

mysql> select * from openchats; 
+----+-------------+------+---------------------+--------------------------------------+---------------------+---------------------+ 
| id | chatroom_id | open | active    | user_id        | created    | modified   | 
+----+-------------+------+---------------------+--------------------------------------+---------------------+---------------------+ 
| 1 |   3 | 1 | 2017-07-31 19:14:33 | 3f189dab-597a-468f-8f6f-ae577a8e05e8 | 2017-07-31 18:38:00 | 2017-07-31 18:38:00 | 
+----+-------------+------+---------------------+--------------------------------------+---------------------+---------------------+ 
1 row in set (0.00 sec) 

任何人都可以向我解釋,我怎樣才能使第二查詢返回的計數爲零?

謝謝...

回答

1

它看起來像你的錯誤是在下面一行:

DATE_SUB(NOW(), interval 15 minute) < 'Openchats.active' 

您DATE_SUB的結果比較字符串「Openchats.active」,而不是Openchats表的活動屬性。嘗試刪除引號並重新運行它:

DATE_SUB(NOW(), interval 15 minute) < Openchats.active