【问题标题】:Checking for empty fields in mysql table检查mysql表中的空字段
【发布时间】:2018-07-24 23:23:48
【问题描述】:

我有一个 12 列和 200 行的表。我想使用 php/mysql 有效地检查此表中为空/null 的字段。例如。 “(第 3 列第 30 行)为空”。有没有可以做到这一点的功能? 简而言之:SELECT * FROM TABLE_PRODUCTS WHERE ANY column has empty fields.

【问题讨论】:

  • 您的表格中没有“第 30 行”。这是一个堆,而不是有序列表。

标签: php mysql sql


【解决方案1】:

empty != null

select * from table_products where column is null or column='';

【讨论】:

    【解决方案2】:
    SELECT * FROM table WHERE COLUMN IS NULL
    

    【讨论】:

      【解决方案3】:

      据我所知,在 MySQL 中没有检查每一列的功能,我猜你必须像这样遍历列...

      $columns = array('column1','column2','column3');
      foreach($columns as $column){
        $where .= "$column = '' AND ";
      }
      $where = substr($where, 0, -4);
      $result = mysql_query("SELECT * FROM table WHERE $where",$database_connection);
      //do something with $result;
      

      = '' 将为您获取空字段。

      【讨论】:

      • 您好,谢谢。有没有办法一次列出所有列,而不是手动列出列?
      【解决方案4】:

      你总是可以尝试这种方法:

      //do connection stuff beforehand
      
      $tableName = "foo";
      
      $q1 = <<<SQL
      SELECT
        CONCAT(
          "SELECT * FROM $tableName WHERE" ,
          GROUP_CONCAT(
            '(' ,
            '`' ,
            column_name,
            '`' ,
            ' is NULL OR ',
            '`' ,
            column_name ,
            '`',
            ' = ""' , ')'
            SEPARATOR ' OR ')
          ) AS foo
      FROM
      information_schema.columns
      WHERE
      table_name = "$tableName"
      
      SQL;
      
      $rows = mysql_query($q1);
      
      if ($rows)
      {
          $row = mysql_fetch_array($rows);
          $q2 = $row[0];
      }
      
      $null_blank_rows = mysql_query($q2);
      
      // process the null / blank rows..
      

      【讨论】:

        【解决方案5】:
        <?php
            set_time_limit(1000);
        
            $schematable = "schema.table";
        
            $desc = mysql_query('describe '.$schematable) or die(mysql_error());
        
            while ($row = mysql_fetch_array($desc)){
        
                $field = $row['Field'];
                $result = mysql_query('select * from '.$schematable.' where `'.$field.'` is not null or `'.$field.'` != ""');
        
                if (mysql_num_rows($result) == 0){
                    echo $field.' has no data <br/>';
                }
        
            }
        ?>
        

        【讨论】:

          【解决方案6】:
          $sql = "SELECT * FROM TABLE_PRODUCTS";
          $res = mysql_query($sql);
          
          $emptyFields = array();
          
          while ($row = mysql_fetch_array($res)) {
              foreach($row as $key => $field) {
                  if(empty($field)) || is_null($field) {
                      $emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $key, $row['table_primary_key']);
                  }
              }
          }
          
          print_r($emptyFields);
          

          未经测试,因此可能有错别字,但这是主要思想。 那就是如果你想确切地知道哪一列是空的还是 NULL。

          此外,在非常大的桌子上执行此操作也不是一种非常有效的方法,但对于 200 行长的桌子应该很快。也许有更简洁的解决方案来处理您的应用程序中的空/空字段,这些解决方案不需要像那样显式检测它们,但这取决于您想要做什么:)

          【讨论】:

          • 谢谢朋友,它现在就像魔术一样工作。只需要稍微格式化一下。节省了我很多时间。
          【解决方案7】:

          检查此代码是否有空字段

          $sql = "SELECT * FROM tablename WHERE condition";
          
          $res = mysql_query($sql);
          
          while ($row = mysql_fetch_assoc($res)) {
          
              foreach($row as $key => $field) { 
                  echo "<br>";
                  if(empty($row[$key])){
                      echo $key." : empty field :"."<br>";        
                  }else{
                      echo $key." =" . $field."<br>";     1
                  }
              }
          
          }
          

          【讨论】:

            【解决方案8】:

            这里我用的是带名字的表

            $show_lang = $db_conx -> query("SHOW COLUMNS FROM words");
                    while ($col = $show_lang -> fetch_assoc()) {
                        $field = $col['Field'];
                        $sel_lan = $db_conx -> query("SELECT * FROM words WHERE $field = '' ");
                        $word_count = mysqli_num_rows($sel_lan);
                        echo "the field ".$field." is empty at:";
                        if ($word_count != 0) {
                          while($fetch = $sel_lan -> fetch_array()){
                            echo "<br>id = ".$fetch['id']; //hope you have the field id...
                          }
                        }
                    }
            

            【讨论】:

              【解决方案9】:

              没有类似的功能,但如果允许使用其他语言,您可以提取表的结构并使用它来生成查询。 如果您只需要对具有 30 列的单个表进行此操作,则手动编写查询会更快...

              【讨论】:

                猜你喜欢
                • 2011-01-20
                • 1970-01-01
                • 2016-10-09
                • 2013-04-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-09-23
                • 1970-01-01
                相关资源
                最近更新 更多