[英]sequelize where date clause
When I run the next statement using Sequelize.js 当我使用Sequelize.js运行下一个语句时
models.TblActivitat.findAll(
{
attributes:
[
'codiAct', 'procedencia', 'pacient', 'proces', 'prova', 'rmn', 'realitzador',
[Sequelize.fn('date_format', Sequelize.col('data'), '%d-%m-%Y'), 'data']
],
include: models.TblTecnics,
where: {"data": '2016-10-20' }, //dataAAAAMMDD
order: "registre DESC"
}).then(function(tblActTec){
...
});
... I'd have to get some records, but I get nothing ...我必须得到一些记录,但我什么都没得到
running on mysql I get the results: 在mysql上运行我得到了结果:
mysql> select data from activitat where data = '2016-10-20' ; mysql>从activitat中选择数据,其中data = '2016-10-20' ;
+------------+ + ------------ +
| | data | 数据|
+------------+ + ------------ +
| | 2016-10-20 | 2016-10-20 |
| | 2016-10-20 | 2016-10-20 |
| | 2016-10-20 | 2016-10-20 |
| | 2016-10-20 | 2016-10-20 |
+------------+ + ------------ +
If I see the console of the Node server. 如果我看到节点服务器的控制台。 When the satement before is executed, it shows as follow: 执行之前的satement时,它显示如下:
Executing (default): SELECT activitat
. 执行(默认):SELECT activitat
。 codiAct
activitat
. codiAct
activitat
。 procedencia
, activitat
. procedencia
, activitat
。 pacient
, activitat
. pacient
, activitat
。 proces
, activitat
. proces
, activitat
。 prova
, activitat
. prova
, activitat
。 rmn
, activitat
. rmn
, activitat
。 realitzador
, date_format( data
, '%d-%m-%Y') AS data
, tecnic
. realitzador
,date_format( data
,'%d-%m-%Y')AS data
, tecnic
。 codiTec
AS tecnic.codiTec
, tecnic
. codiTec
AS tecnic.codiTec
, tecnic
。 nom
AS tecnic.nom
FROM activitat
AS activitat
LEFT OUTER JOIN tecnics
AS tecnic
ON activitat
. nom
AS tecnic.nom
FROM activitat
AS activitat
LEFT OUTER JOIN tecnics
AS tecnic
ON activitat
。 realitzador
= tecnic
. realitzador
= tecnic
。 codiTec
WHERE activitat
. codiTec
WHERE activitat
。 data
= ' 2016-10-19 22:00:00 ' data
=' 2016-10-19 22:00:00 '
ORDER BY registre DESC; ORDER BY注册DESC;
my question is: 我的问题是:
I wrote "2016-10-20" as the where clause. 我写了“2016-10-20”作为where子句。 When I do it in mysql it shows the results I hope, but when sequelize is executed it changes the value of data clause by "2016-10-19 22:00:00" (two hours before "2016-10-20 00:00:00"!!). 当我在mysql中执行它时,它会显示我希望的结果,但是当执行sequelize时,它会将数据子句的值更改为“2016-10-19 22:00:00”(在2016-10-20 00之前两小时: 00:00" !!)。 Why? 为什么?
I remeber you that "data" field is a Date field (not a DateTime field) 我记得你“data”字段是Date字段(不是DateTime字段)
Thank you very very much!! 非常非常感谢你!!
Sequelize converts the string you pass in to a Date object, which has a time assigned to it. Sequelize将您传入的字符串转换为Date对象,该对象具有分配给它的时间。 If you want to select records on a certain date (rather than at an exact time) you can do so like this: 如果要在特定日期(而不是在确切时间)选择记录,可以这样做:
date: {
$lt: new Date('2016-10-20'),
$gt: new Date(new Date('2016-10-20') - 24 * 60 * 60 * 1000)
}
Or in your case: 或者在你的情况下:
models.TblActivitat.findAll(
{
attributes:
[
'codiAct', 'procedencia', 'pacient', 'proces', 'prova', 'rmn', 'realitzador',
[Sequelize.fn('date_format', Sequelize.col('data'), '%d-%m-%Y'), 'data']
],
include: models.TblTecnics,
where: {"data": {
$lt: new Date('2016-10-20'),
$gt: new Date(new Date('2016-10-20') - 24 * 60 * 60 * 1000)
} }, //dataAAAAMMDD
order: "registre DESC"
}).then(function(tblActTec){
...
});
I've discovered a new field type when you define the fields of the table in Sequelize. 当你在Sequelize中定义表的字段时,我发现了一个新的字段类型。 This is the DATAONLY type (instead of DATA type). 这是DATAONLY类型(而不是DATA类型)。 So, the field definition follows as: 因此,字段定义如下:
data:
{
type: DataTypes.DATEONLY,
validate:
{
notEmpty:
{
msg: "-> Falta data"
}
}
},
With this file type (DATAONLY) it only considers de date part (not the time part) 使用此文件类型(DATAONLY)它只考虑日期部分(不是时间部分)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.