如何使用NodeJS模型和mysql处理日期
我在一个使用nodejsapi和mysql作为数据库的项目中工作。 我需要做一个查询,返回当前月份的所有数据,另一个查询返回期间之间的所有数据。 为了得到所有的记录,我正在这样做如何使用NodeJS模型和mysql处理日期,mysql,node.js,Mysql,Node.js,我在一个使用nodejsapi和mysql作为数据库的项目中工作。 我需要做一个查询,返回当前月份的所有数据,另一个查询返回期间之间的所有数据。 为了得到所有的记录,我正在这样做 ContractValue.findAll({ where: { data: id }, order: [['data', 'ASC']] }) 问题是我不知道如何将条件放入where子句中,该子句用于处理mysql数据库,首先创建mysql连接对象: va
ContractValue.findAll({
where: {
data: id
},
order: [['data', 'ASC']]
})
问题是我不知道如何将条件放入where子句中,该子句用于处理mysql数据库,首先创建mysql连接对象:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
然后,您可以使用它构建所需的查询:
connection.query('SELECT * FROM table WHERE 'YOU CONDITIONS...', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
这似乎是你在使用“Sequelize”作为你的ORM,从你的问题中我可以看出问题在于你不知道如何把条件放在哪里 这应该对你有帮助
ContractValue.findAll({
其中:{
身份证:{
[Op.and]:{a:5}、//和(a=5)
[Op.or]:[{a:5},{a:6}],//(a=5或a=6)
[Op.gt]:6,//id>6
[Op.gte]:6,//id>=6
[Op.lt]:10,//id<10
[Op.lte]:10,//id[1,2](PG阵列包含运营商)
[Op.contained]:[1,2],//您能指定您正在使用的ORM吗?
ContractValue.findAll({
where: {
id: {
[Op.and]: {a: 5}, // AND (a = 5)
[Op.or]: [{a: 5}, {a: 6}], // (a = 5 OR a = 6)
[Op.gt]: 6, // id > 6
[Op.gte]: 6, // id >= 6
[Op.lt]: 10, // id < 10
[Op.lte]: 10, // id <= 10
[Op.ne]: 20, // id != 20
[Op.between]: [6, 10], // BETWEEN 6 AND 10
[Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
[Op.in]: [1, 2], // IN [1, 2]
[Op.notIn]: [1, 2], // NOT IN [1, 2]
[Op.like]: '%hat', // LIKE '%hat'
[Op.notLike]: '%hat', // NOT LIKE '%hat'
[Op.iLike]: '%hat', // ILIKE '%hat' (case insensitive) (PG only)
[Op.notILike]: '%hat', // NOT ILIKE '%hat' (PG only)
[Op.overlap]: [1, 2], // && [1, 2] (PG array overlap operator)
[Op.contains]: [1, 2], // @> [1, 2] (PG array contains operator)
[Op.contained]: [1, 2], // <@ [1, 2] (PG array contained by operator)
[Op.any]: [2,3] // ANY ARRAY[2, 3]::INTEGER (PG only)
},
status: {
[Op.not]: false // status NOT FALSE
}
}
})
ContractValue.findAll({
where: {
data: {
[Op.between]: [start, end]
}
},
order: [
['data', 'ASC']
]
})