简体   繁体   English

计算数据库中所有列等于“ YES”的行

[英]Count all rows in database where column equals to `YES`

I have a database crm_data in which I have multiple tables. 我有一个数据库crm_data ,其中有多个表。 Now I want to calculate all rows in whole database where column_name value is equal to YES . 现在,我要计算整个数据库中column_name值等于YES所有行。 To calculate all rows in database I am using this mysql query. 为了计算数据库中的所有行,我正在使用此mysql查询。

My SQL Query: 我的SQL查询:

$sql = $db_con2->prepare("SELECT SUM(TABLE_ROWS) AS all_rows FROM 
       INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'crm_data'");
$sql->execute();
$all_rows = $sql->fetchAll();
if (count($all_rows) > 0) {
   foreach ($all_rows as $all_rows) {
      echo $all_rows['all_rows'];
   }
} else {
   $all_rows = '0';
}

Thanks 谢谢

What I did I went back to basics and used SHOW TABLES then I use table query SELECT * FROM $company WHERE rental_status = 'YES' . 我做了什么,回到基础并使用SHOW TABLES然后使用表查询SELECT * FROM $company WHERE rental_status = 'YES' and I got the results. 我得到了结果。

BTW thanks guys giving me your suggestion. 顺便说一句,谢谢你们给我的建议。

Try this, i hope it works for you 试试这个,我希望它对你有用

// connect to Database.
   $links = mysqli_connect($db_host, $db_username, $db_password, $db_name );

   if($links === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
      }

//get total
    $n = 'YES';



$result = mysqli_query($links,"SELECT   Count(*) As column_name FROM crm_data WHERE column_name='".$n."'");

                                                       $rows = mysqli_num_rows($result);



                                                if($rows){

                                                    $gt = mysqli_fetch_assoc($result);

                                                     $total = $gt["column_name"];

                                                    }
                                   echo $total;

Using PDO as requested. 根据要求使用PDO。

 // using PDO
  $servername = "localhost";
$username = "user";
$password = "pass";

try {
    $conn = new PDO("mysql:host=$servername;dbname=dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo "Connected successfully"; 

// call for the row count here.

$w='YES';
$rs = $conn->prepare("SELECT   Count(*) As column_name FROM crm_data WHERE column_name='".$w."'");
$rs->execute();
$count = $rs->rowCount();
echo '<p>Total:'.$count.'</p>';
}
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }   

You're not querying the data in your database you're now querying the actual structure of the database, which is concerning. 您不是要查询数据库中的数据,而是要查询数据库的实际结构。

Nevertheless... 不过...

 SELECT SUM(`COLUMN_NAME`) FROM `INFORMATION_SCHEMA`.`COLUMNS` where `COLUMN_NAME` = 'YES' AND `TABLE_SCHEMA` = 'crm_data';

Is the query you are looking for... 是您要查找的查询...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL获取另一个表中所有行的计数,其中值等于当前表中的列 - MySQL get count of all rows in another table where the value equals column in current table 如何计算等于相同ID的所有行中的列? - How to count columns from all rows where it equals the same id? 如何选择计算所有和所有行,并且仅在列不为空的情况下回显 - How to select count all and rows and only echo where column is not empty MYSQL:如何检索所有表行+仅检索列等于的最后一行 - MYSQL: how to retrieve all table rows + only the last row where column equals PDO-返回日期列等于本月的所有行,但是日期格式为unix时间戳 - PDO - Return all rows where date column equals this month, however date is formatted as unix timestamp MySQL 统计表中的行数 WHERE userid 等于? - 代码点火器 - MySQL Count rows From Table WHERE userid equals? - CodeIgniter 单个查询可获取给定数据库中所有表名,行数和状态为“ 1”的行数 - Single query to get all the table name, row count and count of rows where status is '1' in a given database 选择count()等于特定值的所有用户 - select all users where count() equals a specific value 选择列名称不等于多个值的行 - Select rows where column name not equals multiple values 选择laravel中价格总和等于或1000的所有行 - Select all rows where the sum of price equals or 1000 in laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM