查看mysql请求是否为Null,Empty set或其中是否包含某些内容

时间:2019-02-18 19:54:17

标签: mysql sql node.js

我想知道如何使用节点JS(在mysql选择请求为NULL,为空集或其中包含某些内容的情况下)获得条件。

目前我拥有:

function checkKey(key, cb) {
    var activated = "";
    var sqlcheck = "SELECT activated from authentification where discord_key = ?";
    console.log("in function");
    DB.query(sqlcheck, [key], function (err, result) {
        if (err) throw (err);
        if (result[0] && result[0].activated) {
            activated = result[0].activated;
            console.log("filled key");
        } else {
            activated = false;
            console.log("empty key");
        }
        cb(activatedkey(activated));
        //return (activatedkey(activated));
    })
}

有了这个,我只能得到Empty and Null或其他东西。 我正在寻找一种方法来获取EMPTY或NULL或其中的任何内容。

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以在身份验证表中放置第二行,并尽可能增加 键。因此,您都可以在两行中都使用OR,然后通过键对其进行排序,将其限制为1。

SELECT activated
FROM authentification
WHERE discord_key = 123456
OR discord_key = 9999999
ORDER BY discord_key LIMIT 1;

样品

    MariaDB [test_arduino]> SELECT * FROM authentification;
    +----+-----------+-------------+
    | id | activated | discord_key |
    +----+-----------+-------------+
    |  1 | 1         | 12345       |
    |  2 | EMPTY     | 9999999     |
    +----+-----------+-------------+
    2 rows in set (0.001 sec)

MariaDB []> SELECT activated
        ->     FROM authentification
        ->     WHERE discord_key = 12345
        ->     OR discord_key = 9999999
        ->     ORDER BY discord_key LIMIT 1;
    +-----------+
    | activated |
    +-----------+
    | 1         |
    +-----------+
    1 row in set (0.001 sec)

    MariaDB []> SELECT activated
        ->     FROM authentification
        ->     WHERE discord_key = 123456
        ->     OR discord_key = 9999999
        ->     ORDER BY discord_key LIMIT 1;
    +-----------+
    | activated |
    +-----------+
    | EMPTY     |
    +-----------+
    1 row in set (0.001 sec)

    MariaDB []>