我想在我的查询中添加一个where()
子句,但是conditionally.具体来说,我只希望在URL中传递sepecific querystring参数时添加它.这可能吗?如果可能的话,我该怎么做?
router.get('/questions', function (req, res) {
knex('questions')
.select('question', 'correct', 'incorrect')
.limit(50)
.where('somecolumn', req.query.param) // <-- only if param exists
.then(function (results) {
res.send(results);
});
});