【问题标题】:How apply where only a column is not null in adonis query builder如何在阿多尼斯查询生成器中只有一列不为空的情况下应用
【发布时间】:2020-01-15 12:27:00
【问题描述】:

我需要检查列 percentage_correct >= parCorrect if percentage_correct NULL

我尝试类似:

const unidadeSequenceAtualEstudante = await Database.select("*")
            .from("student_quiz_historics")
            .where("student_id", idEstudante)
            .where(function(){
                this
                .whereNotNull("percentage_correct")
                .where("percentage_correct", ">=", parQtdAcerto)
            })
            .where("class_id", idClasse)
            .where("book_id", idLivro)
            .where("execution_back_status", "<>", "Cancelado")
            .orderBy("sequence", "desc")
            .first();

但是当我只有percentage_correct null 的记录时,他们仍然试图将其应用到哪里。

【问题讨论】:

标签: node.js knex.js adonis.js


【解决方案1】:

我在复制您的问题时遇到了一点麻烦。你所拥有的应该可以工作,但你可能没有考虑关于 NULL 的 SQL 行为:anything 与 NULL 相比是 NULL,这就是 IS NOT NULL 语法存在的原因。比如:

foo=# SELECT 1 >= NULL;
?column?
----------
NULL
(1 row)

foo=# SELECT NULL >= 1;
?column?
----------
NULL
(1 row)

这意味着您可以完全不检查 null,因为只会返回满足条件的行:

const unidadeSequenceAtualEstudante = await Database.select("*")
  .from("student_quiz_historics")
  .where({
    book_id: idLivro,
    class_id: idClasse,
    student_id: idEstudante
  })
  .where('percentage_correct', '>=', parQtdAcerto)
  .where("execution_back_status", "<>", "Cancelado")
  .orderBy("sequence", "desc")
  .first();

这是个好主意吗?值得商榷。我认为这可能很好,但我们不应该假设NULLFALSE 相同,因为它不是。

如果您的查询仍有问题,您需要提供更多详细信息,说明您使用的是哪个数据库、您的架构是什么以及您遇到的错误类型。

【讨论】:

    猜你喜欢
    • 2017-03-30
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 2020-04-11
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多