MySQL SELECT 使用 WHERE 和 BETWEEN

MySQL SELECT using WHERE & BETWEEN

我有几个具有相同结构的数据库:

dsid、标签、时间戳、值

时间戳是 unixtime,我可以 select 数据这样绘制:

SELECT CONCAT(t.`dsid` ,' - ', t.`dsid` - 1) AS `DSIDs`
 , t.`value` - (SELECT `value`
                FROM `MY-DB` 
                WHERE `dsid` = t.`dsid` - 1 * 20) AS `kWh`,
                tag, dsid, value,
                FROM_UNIXTIME(timestamp, '%y-%d-%m %H:%i')
FROM `MY-DB` t limit 10000;

但我想select它基于"where date between"结构,所以我可以选择月或周...或者更好的是"last"周、月、年(完整日历年或仅过去 12 个月)。

但是如果我那样尝试它会失败:

SELECT CONCAT(t.`dsid` ,' - ', t.`dsid` - 1) AS `DSIDs`
 , t.`value` - (SELECT `value`
                FROM `MY-DB` 
                WHERE `dsid` = t.`dsid` - 1 * 20) AS `kWh`,
                tag, dsid, value, (FROM_UNIXTIME(timestamp, '%y-%d-%m %H:%i') WHERE 'timestamp' BETWEEN UNIX_TIMESTAMP('16-27-03 23:45')) AND UNIX_TIMESTAMP('16-25-02 00:00')
FROM `MY-DB` t limit 10000;

怎么了?

错误的是我将 WHERE 和 BETWEEN 放在了错误的位置,这对我来说是这样的:

SELECT CONCAT(t.`dsid` ,' - ', t.`dsid` - 1) AS `DSIDs` , t.`value` - (SELECT `value`
            FROM `MYDB` 
            WHERE `dsid` = t.`dsid` - 1 * 20) AS `kWh`,
            tag, dsid, value,
            FROM_UNIXTIME(timestamp, '%y-%m-%d %H:%i') as time
FROM `MYDB` t
WHERE timestamp
BETWEEN UNIX_TIMESTAMP('16-08-01') AND UNIX_TIMESTAMP('16-08-31 23:59:59')

可能不是最有效的方法..