2017-08-28 150 views
1

與組的最高時間戳這是我的查詢:使MySQL查詢選擇通過聲明

SELECT 
    log_timestamp, 
    system_name, 
    faction_name, 
    faction_allegiance, 
    faction_government_type, 
    current_state, 
    pending_state, 
    recovering_state, 
    influence AS influence, 
    transactions.transaction_id 
FROM `bgs`.`faction_log` 
    INNER JOIN `factions` ON `factions`.`faction_id` = `faction_log`.`faction_id` 
    INNER JOIN `transactions` ON `transactions`.`transaction_id` = `faction_log`.`transaction_id` 
    INNER JOIN `system_log` ON `system_log`.`transaction_id` = `transactions`.`transaction_id` 
    INNER JOIN `systems` ON `systems`.`system_id` = `transactions`.`associated_system_id` 
WHERE 
    log_timestamp BETWEEN bgs.find_closest_tick_time(date('2017-08-14')) AND bgs.find_closest_tick_time(DATE_ADD(date('2017-08-14'), INTERVAL 1 DAY)) AND 
    associated_system_id = 4 
GROUP BY 
    faction_name, 
    log_timestamp, 
    system_name, 
    faction_allegiance, 
    faction_government_type, 
    current_state, 
    pending_state, 
    recovering_state, 
    influence, 
    transactions.transaction_id 

它工作正常,現在看起來是這樣的:

+---------------------+-------------+-----------------+ | log_timestamp | system_name | faction_name | +---------------------+-------------+-----------------+ | 2017-08-14 11:18:46 | Nagii | Nagii Autocracy | | 2017-08-14 12:38:03 | Nagii | Nagii Autocracy | | 2017-08-14 13:40:18 | Nagii | Nagii Autocracy | | 2017-08-15 06:09:52 | Nagii | Nagii Autocracy | | 2017-08-14 11:18:46 | Nagii | Nagii Network | | 2017-08-14 12:38:03 | Nagii | Nagii Network | | 2017-08-14 13:40:18 | Nagii | Nagii Network | | 2017-08-15 06:09:52 | Nagii | Nagii Network | | 2017-08-14 11:18:46 | Nagii | Nagii Posse | | 2017-08-14 12:38:03 | Nagii | Nagii Posse | | 2017-08-14 13:40:18 | Nagii | Nagii Posse | | 2017-08-15 06:09:52 | Nagii | Nagii Posse | | 2017-08-14 11:18:46 | Nagii | Nagii Union | | 2017-08-14 12:38:03 | Nagii | Nagii Union | | 2017-08-14 13:40:18 | Nagii | Nagii Union | | 2017-08-15 06:09:52 | Nagii | Nagii Union | | 2017-08-14 11:18:46 | Nagii | Nagii United Co | | 2017-08-14 12:38:03 | Nagii | Nagii United Co | | 2017-08-14 13:40:18 | Nagii | Nagii United Co | | 2017-08-15 06:09:52 | Nagii | Nagii United Co | +---------------------+-------------+-----------------+ 20 rows in set (0.00 sec)

但我想唯一選擇包含字段log_timestamp的最大值的行。 (在這種情況下2017-08-15 06:09:52)。我試圖用類似的東西:

HAVING transactions.log_timestamp >= max(date('2017-08-15 06:09:52'));

,但它不喜歡的工作:

HAVING transactions.log_timestamp >= max(log_timestamp);

我到底做錯了什麼?

回答

0

這裏是另一種解決方案。如果您只希望在選擇列表的所有列中爲每個不同值創建一行,則不需要GROUP BY。你可以做同樣的事情SELECT DISTINCT如下:

SELECT DISTINCT 
    log_timestamp, 
    system_name, 
    faction_name, 
    faction_allegiance, 
    faction_government_type, 
    current_state, 
    pending_state, 
    recovering_state, 
    influence AS influence, 
    transactions.transaction_id 
FROM `bgs`.`faction_log` 
    INNER JOIN `factions` ON `factions`.`faction_id` = `faction_log`.`faction_id` 
    INNER JOIN `transactions` ON `transactions`.`transaction_id` = `faction_log`.`transaction_id` 
    INNER JOIN `system_log` ON `system_log`.`transaction_id` = `transactions`.`transaction_id` 
    INNER JOIN `systems` ON `systems`.`system_id` = `transactions`.`associated_system_id` 
WHERE 
    log_timestamp BETWEEN bgs.find_closest_tick_time(date('2017-08-14')) AND bgs.find_closest_tick_time(DATE_ADD(date('2017-08-14'), INTERVAL 1 DAY)) AND 
    associated_system_id = 4 
ORDER BY log_timestamp DESC 
LIMIT 1 

既然你只是想最大log_timestamp行,你可以通過柱降下來,使用極限排序,結果中只返回第一行。

+0

什麼是最適合大桌子上的性能? –

+0

性能最好的方法是使用索引優化表。您沒有爲您的表發佈「SHOW CREATE TABLE」,也不知道「log_timestamp」屬於哪個表,因此我無法評論哪些索引是需要的。 –

1

你可以嘗試一個子查詢,像

WHERE 
    log_timestamp = (
     SELECT MAX(log_timestamp) 
     FROM `bgs`.`faction_log` 
     WHERE log_timestamp BETWEEN bgs.find_closest_tick_time(date('2017-08-14')) AND bgs.find_closest_tick_time(DATE_ADD(date('2017-08-14'), INTERVAL 1 DAY)) AND associated_system_id = 4 
) AND associated_system_id = 4 
+0

有語法錯誤,在HAVING部分使用這個子查詢工作,並加入當然內部連接=) –

+0

Thx幫助,塞巴斯蒂安 –