首页 > 解决方案 > 如何在 knex.js 中过滤我的查询以获取较少的计数和表中的空单元格?

问题描述

我正在尝试使用 knexjs 查询来连续获取一个空单元格,然后如果它返回多行,我会选择每行中某个值的较少计数。

这意味着我检查每一行的“一些值”

如果我有 2 行,但第一行有 4 个“某个值”的单元格,第二行有 2 个该值的单元格,我选择第二行。同时我记住空单元格作为我的条件。

我试着这样做

knexDb('test_table')          
  .where(function() {
    this.where('col1', '=', '')
    .orWhere('col2', '=', '')
    .orWhere('col3', '=', '')
    .orWhere('col4', '=', '')
    .orWhere('col5', '=', '')
  })
  .orderByRaw('random()')
  // here i get all the availabe rows with empty cells

  // then here i want to do some check to compare all rows with the lesser count of "some value"

请记住,我通过执行 col1、col2、col3 等检查行中的每一列......

这意味着我需要对返回值的引用并跟踪每行中存在的该值的计数。

================================================

更新

================================================

test_table 有 3 行,如下所示:

{
    id: 1,
    col1: '',
    col2: '',
    col3: 'sasha',
},
{
    id: 2,
    col1: '',
    col2: 'sasha',
    col3: 'sasha',
},
{
    id: 3,
    col1: 'another_name',
    col2: 'another_name',
    col3: 'another_name',
}

上面的查询将返回前 2 个对象,因为它们有空单元格

现在所需的输出是选择并返回第一个对象,因为它的 (sasha) 计数较少并且单元格为空

标签: node.jspostgresqlknex.js

解决方案


你的问题分为两部分:

  1. 如何查询?
SELECT *, 
       ( SELECT 
           (CASE WHEN col1='sasha' THEN 1 ELSE 0 End) + 
           (CASE WHEN col2='sasha' THEN 1 ELSE 0 End) +
           (CASE WHEN col3='sasha' THEN 1 ELSE 0 End))
         FROM   test_table AS INNER 
         WHERE  inner.id = test_table.id
       ) AS counter 
FROM   test_table 
WHERE  col1 = '' 
        OR col2 = '' 
        OR col3 = '' 
ORDER  BY counter ASC 
LIMIT  1 
  1. 如何构建此 SQL 查询knex
const columns = ['col1', 'col2', 'col3', 'col4', 'col5'];

knexDb('test_table')
  .columns([
    '*',
    knexDb('test_table as inner')
      .column(
        knexDb.raw(
          '(' +
            columns.map((col) => `(CASE WHEN ${col}='sasha' THEN 1 ELSE 0 End)`).join(' + ') +
            ') as inner_counter'
        )
      )
      .where('inner.id', '=', 'test_table.id')
      .orderBy('inner_counter')
      .limit(1)
      .as('counter'),
  ])
  .where((qb) => {
    columns.forEach((col) => {
      qb.orWhere(col, '=', '');
    });
  })
  .orderByRaw('random()');


推荐阅读