Egg: mysql如何模糊查询

Created on 18 May 2017  ·  2Comments  ·  Source: eggjs/egg

教程里看到下面这个mysql 的使用方式,不过想补充稳一点,如何配置 like 的模糊查询

条件查询和结果定制
const results = yield this.app.mysql.select('posts', { // 搜索 post 表
where: { status: 'draft', author: ['author1', 'author2'] }, // WHERE 条件
columns: ['author', 'title'], // 要查询的表字段
orders: [['created_at','desc'], ['id','desc']], // 排序方式
limit: 10, // 返回数据量
offset: 0, // 数据偏移量
});
=> SELECT author, title FROM posts
WHERE status = 'draft' AND author IN('author1','author2')
ORDER BY created_at DESC, id DESC LIMIT 0, 10;

All 2 comments

自己写 SQL 查询就可以了。

经常会有like的需求 增加一个like字段不行么

Was this page helpful?
0 / 5 - 0 ratings