根据参数有条件地修改查询

时间:2019-08-01 21:40:56

标签: sql sql-server

我有这个查询(就像一个case语句,我可以使用并修复它)

select *
from mytable
where 1=1 
and (isNull(ID, 0) = 0 OR UtilityID IN (9,40)) 

我也想添加另一条语句

select *
from mytable
where 1=1 
and UtilityID NOT IN (9,40) 

过程中发生的所有事情都在发生,因此要使用declare @something之类的变量,因此如果将其作为1传递,请使用第一条语句,如果if 0传递,请使用后者。

2 个答案:

答案 0 :(得分:1)

当我欣赏Dale的回答中的天才时,我发现它更具可读性:

IF @something = 0 
BEGIN
  select *
  from mytable
  where ID IS NULL OR ID = 0 OR UtilityID IN (9,40);
END
IF @something = 1
BEGIN
  select *
  from mytable
  where UtilityID NOT IN (9,40);
END

这是过程代码,因此使用IF来控制流程。还扩展并简化了where子句

答案 1 :(得分:0)

我想我理解您的逻辑,忽略了您只想在variables: { where: { league: { name_contains: "some league name" }, teams: { name_contains: "some teams name" } } } 时才允许const SEARCH_QUERY = gql` query SEARCH_QUERY($where: JSON) { games(where: $where) { id teams { id name } league { id name } } } 的{​​{1}}(什么都不做)。应该这样做:

const router = require("express").Router()
const mailer = require('../controllers/mailerController')

router
    .route('/your-end-point-here')
    .post(mailer.addToMailList)

module.exports = router

对于较大的查询,性能更高的方法可能是:

const nodemailer = require('nodemailer')


module.exports =
{
    addToMailList: function (req, res)
    {
        let { newUser } = req.body

        let transporter = nodemailer.createTransport({
            service:'gmail',
            auth: {
                user: 'yourcompanyemail@gmail.com',
                pass: 'somepassword'
            }

        });

        message = "You just logged in!"

        // setup email data with unicode symbols
        let mailOptions = {
            from: '"Your Company Name" <yourcompanyemail@gmail.com>', // sender address
            to: newUser, // list of receivers
            subject: "Welcome Letter", // Subject line
            text: message // plain text body
        };

        // send mail with defined transport object
        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                return
                console.log(error);
            }
            res.json(info);

        });


    }

}

PS:希望您的1=1永远不会id = 0,它应该对此有所限制。