2013-11-15 60 views
0

我想在兩天中減去在session ='morning'中輸入的anem_reading值,i.date = today和x.date昨天值顯示爲'run',但sql中出錯代碼是THREMysql - 從前一天減值減值

SELECT x.date, 

    i.anem_reading - (SELECT anem_reading 
       FROM reg_data x 
       WHERE date = DATE_SUB(now(), INTERVAL 1 DAY) 
       AND x.(session=morning) = (i.session=morning) 
       ORDER BY date 
       LIMIT 1) run FROM reg_data i; 



    CREATE TABLE `reg_data` (
    `date` varchar(10) NOT NULL, 
    `session` varchar(10) NOT NULL, 
    `time` time default NULL, 
    `temp_air` float NOT NULL, 
    `rel_humid` float NOT NULL, 
    `soil_temp_5` float NOT NULL, 
    `soil_temp_20` float NOT NULL, 
    `soil_temp_30` float NOT NULL, 
    `soil_temp_60` float NOT NULL, 
    `air_pressure` float NOT NULL, 
    `anem_reading` float NOT NULL, 
    `dry_temp` float NOT NULL, 
    `wet_temp` float NOT NULL, 
    PRIMARY KEY (`date`,`session`) 
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1; 
+0

是'FLOAT'一個德爾iberate選擇? – Strawberry

+0

我不認爲'i.anem_reading - (select X)FROM FROM'什麼都是有效的MySQL語法。你缺少部分查詢或有一些括號問題? (甚至只是誤解'AS運行')? – 2013-11-15 17:07:59

+0

@ebyrob是的這是myquery請給我幫助godd查詢 – user2986352

回答

0

翻譯的風格(希望精確地)相同的查詢:

SELECT 
    /**/x.date,/**/ -- this is a bug and should be fixed (i?) 
    i.anem_reading - (
    SELECT x.anem_reading 
    FROM reg_data AS x 
    WHERE x.date = DATE_SUB(now(), INTERVAL 1 DAY) -- relative to i.date? 
     AND (x.session = x.morning) = (i.session = i.morning) 
    ORDER BY x.date -- should be DESC? 
    LIMIT 1 
) AS run 
FROM reg_data AS i 
; 
+0

不太好,謝謝你的友善請求我的朋友 – user2986352

1

請試試這個:

select a.dateToday, i.anem_reading - a.anem_reading 
from    
(    
SELECT anem_reading 
FROM reg_data x 
WHERE date = DATE_SUB(now(), INTERVAL 1 DAY)    
    and session='morning' 
    limit 1 
) a, 
    reg_data i 
where i.session = 'morning' 
+0

Sheesh你是怎麼知道早上是一個字符串,而不是一列... – 2013-11-15 17:42:04

+0

@Alexander不是很好的工作錯誤....但對於yoyr類請求thnks – user2986352