我已经在MySQL中存储了一些唯一的代码作为数组,分别针对用户的电子邮件,现在我想验证用户的电子邮件和用户提交的唯一代码。我想创建一个查询,在那里我可以匹配电子邮件和存储在数据库中的唯一id,以继续用户。
数据库条目:
["BZFeWwnmr8Rm6tuu","daFJWZCEtp2WzxtD","VV80UQQZ1ym77h0m"]我已经试过FIND_IN_SET了
这是API的代码,我对用户输入的数据进行了字符串化,如果存储了一个唯一的值,它将返回值,但如果我获取唯一代码的数组,例如,["BZFeWwnmr8Rm6tuu","daFJWZCEtp2WzxtD","VV80UQQZ1ym77h0m"] MySQL查询不起作用。
exports.vr_client_detail = function (req, res) {
const JSON_DATA = 'application/json';
if(req.headers['content-type'] === JSON_DATA){
if (req.body) {
var unique_string = JSON.stringify(req.body.unique_string);
var email = req.body.management_email;
db.sequelize.query('SELECT * FROM im_vr_client_activation '+
'WHERE FIND_IN_SET(unique_string, '+"'"+unique_string+"') AND "+
' management_email= ' + "'" + email + "'").then(function(app){
var arr = app[0];
return res.json({response_status:'success', response_code:185, data:arr, response:'Successfully fetch data.'})
});
}else{
return res.json({response_status:'error',response_code:10001,response:'Post data is empty.'});
}
}else{
return res.json({response_status:'error',response_code:10000,response:'Post data is not a json type.'});
}
}数据不返回任何内容
{
"response_status": "success",
"response_code": 185,
"data": [],
"response": "Successfully fetch data."
}发布于 2019-09-13 16:55:02
最好是规范化您的模式,使用多对一表来保存所有唯一的字符串,而不是将它们放入单个列中。
但是,如果您不能这样做,则需要删除[和]字符,以便可以使用FIND_IN_SET()。
您还应该使用参数化查询来防止SQL注入。请参阅How to create prepared statements in Sequelize?
db.sequelize.query('SELECT * FROM im_vr_client_activation '+
'WHERE FIND_IN_SET(:unique_string, SUBSTR(unique_string, 2, LENGTH(unique_string)-2)) ' +
'AND management_email = :email', {
replacements: { unique_string: unique_string, email: email }
}).then(...)如果您使用的是MySQL 5.7或更高版本,则可以将列的数据类型更改为JSON并使用JSON_CONTAINS()。在8.0中,您可以使用MEMBER OF操作符。
https://stackoverflow.com/questions/57919685
复制相似问题