MySQL SELECT使用WHERE&之间

时间:2017-12-02 23:51:11

标签: mysql unix-timestamp

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

dsid,tag,timestamp,value

时间戳是unixtime,我可以选择要绘制的数据:

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;

但是我想根据“之间的日期”结构来选择它,所以我可以选择一个月或一周......或者甚至更好的“最后”周,月,年(完整日历年或仅仅是最后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;

有什么问题?

1 个答案:

答案 0 :(得分:0)

错误的是我放置了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')

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