runoops.com

Sequelize 区分大小写查询 — Using BINARY

mysql 查询语句如下:

 SELECT
        `uid`,
        `username`
    FROM
        `users` AS `users`
    WHERE
        BINARY 
        `users`.`username` IN ('hammad', 'sAad')
    AND `users`.`status` = 'ACTIVE'
    ORDER BY
        FIELD(
            `username`,
            'hammad',
            'saad.ahmed'
        )
    LIMIT 20;

squelize 区分大小写查询写法:

db.models.users.findAll({

            attributes: ['uid', 'username'],
            where: {
                username: db.sequelize.where(
                    db.sequelize.literal('BINARY username IN ('),
                    `'${users.join("', '")}'`,
                    db.sequelize.literal(')'),
                ),
                status: 'ACTIVE'
            },
            order: !_.isEmpty(users) ? [[db.sequelize.fn('FIELD', db.sequelize.col('username'), ...users)]] : [],
            limit: 20
        });