如何使用node-orm2在MySQL表中搜索字符串?

时间:2022-05-22 21:43:32

I want to search through a table for a particular string. I can find the row when the tag matches the contents exactly. But I want to find all occurrences of "red" even when the tags column has multiple colors in a string like "blue, green, purple, red". Is it possible to use SQL to query? I found a vague reference involving chaining but it wasn't clear how to make it work in this case.

我想在表格中搜索特定的字符串。当标签与内容完全匹配时,我可以找到该行。但是我想找到所有出现的“红色”,即使标签列在字符串中有多种颜色,如“蓝色,绿色,紫色,红色”。是否可以使用SQL进行查询?我找到了一个涉及链接的模糊引用,但在这种情况下如何使其工作尚不清楚。

Here's what I have for the exact match...

这就是我对完全匹配所拥有的......

app.post("/admin/search", function(req, res) {
    var data = req.body;
    req.models.images.find({tags:data.tags}, function(err, results) {
         res.send(results); //Returns on perfect match
    });
}

1 个解决方案

#1


0  

This seems to work.

这似乎有效。

    app.post("/admin/search", function(req, res) {
        var data = req.body;
        req.models.images.find().where("tags LIKE ?",["%"+data.tags+"%"]).run(function(err,results){
            res.send(results);
        });
    });

#1


0  

This seems to work.

这似乎有效。

    app.post("/admin/search", function(req, res) {
        var data = req.body;
        req.models.images.find().where("tags LIKE ?",["%"+data.tags+"%"]).run(function(err,results){
            res.send(results);
        });
    });