Codeigniter-计算连续空列的数量

时间:2018-06-21 15:48:32

标签: mysql codeigniter activerecord null

我是Codeigniter的新手,正在从事需要帮助的项目。

我想计算字段为空的单行的列数。我知道这可以在普通的SQL语句中完成。但是我想学习如何在CI中做到这一点。

我在这个社区搜索了很多小时,但发现了一些意外的答案。

是的,我只能使用此语句对一列执行此操作:

$this->db->where('column_name' =>  NULL)

这是针对单列的。我是否必须对每一列都这样做?或者我可以用更好的方式做到这一点。

请帮助我解决此问题。任何帮助将不胜感激。

谢谢。
问候,
Vaibhav M

1 个答案:

答案 0 :(得分:0)

如果您只处理一行,那么使用以下查询就是这种情况

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title')->unique();
            $table->string('slug')->unique()->nullable();
            $table->text('body');
            $table->string('image')->nullable();
            $table->unsignedInteger('views')->default(0);
            $table->unsignedInteger('category_id')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

然后您可以像这样在$row = $this->db ->get_where('table', ['id' => $value]) ->row_array(); 数组中计算NULL值

$row

查询取决于具有唯一值的$count = 0; foreach ($row as $column) { if(! isset($column)) { $count++; } } echo $count; 列。

如果您要检查多个行,请告诉我,我将其添加到答案中。

相关问题