【问题标题】:Counting specific rows after dynamical WHERE clause query in PDO在 PDO 中动态 WHERE 子句查询后计算特定行数
【发布时间】:2021-01-08 18:03:08
【问题描述】:

我正在关注this tutorial,在 PDO 中进行动态 WHERE 子句查询。

假设我有一个简短的教程代码,就像这样:



// always initialize a variable before use!
$conditions = [];
$parameters = [];

// conditional statements
if (!empty($_GET['name']))
{
   // here we are using LIKE with wildcard search
   // use it ONLY if really need it
   $conditions[] = 'name LIKE ?';
   $parameters[] = '%'.$_GET['name']."%";
}

if (!empty($_GET['sex']))
{
   // here we are using equality
   $conditions[] = 'sex = ?';
   $parameters[] = $_GET['sex'];
}


// the main query
$sql = "SELECT * FROM users";

// a smart code to add all conditions, if any
if ($conditions)
{
   $sql .= " WHERE ".implode(" AND ", $conditions);
}

// the usual prepare/execute/fetch routine
$stmt = $pdo->prepare($sql);
$stmt->execute($parameters);
$data = $stmt->fetchAll();

通过添加行$count = $stmt->rowCount();,我可以计算行数。

现在假设我有这样的数据:

| ID | User name    | sex     |
| -- | ------------ | --------|
| 1  | Sandra Smith | female  | 
| 2  | Ben Smith    | male    | 
| 3  | John Lee     | male    | 
| 4  | John Smith   | male    | 

如果我现在搜索“Smith”,我会得到 3 个结果,当我回显 $count 时,会显示数字 3。

问题: 我还想回应我通过完全相同的查询获得了多少女性和男性用户。 我在互联网上进行了很多搜索,但找不到使用 PDO 解决此问题的案例。另外,我不确定是否需要另一个额外的查询来获取女性和男性用户的数量。

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    在查询中使用 CASE 语句根据条件获取行数:

    <?php
    // always initialize a variable before use!
    $conditions = [];
    $parameters = [];
    
    // conditional statements
    if (!empty($_GET['name']))
    {
       // here we are using LIKE with wildcard search
       // use it ONLY if really need it
       $conditions[] = 'name LIKE ?';
       $parameters[] = '%'.$_GET['name']."%";
    }
    
    if (!empty($_GET['sex']))
    {
       // here we are using equality
       $conditions[] = 'sex = ?';
       $parameters[] = $_GET['sex'];
    }
    
    
    // the main query
    $sql = "SELECT count(*) as count_all,
    sum(case when sex  = 'female ' then 1 else 0 end) AS count_female,
    sum(case when sex  = 'male ' then 1 else 0 end) AS count_male
    FROM users";
    
    // a smart code to add all conditions, if any
    if ($conditions)
    {
       $sql .= " WHERE ".implode(" AND ", $conditions);
    }
    
    // the usual prepare/execute/fetch routine
    $stmt = $pdo->prepare($sql);
    $stmt->execute($parameters);
    $row = $stmt->fetch();
    
    // count all users
    $count_all = $row['count_all'];
    
    // count female users
    $count_all = $row['count_female'];
    
    // count male users
    $count_all = $row['count_male'];
    
    ?>
    

    检查:How to get multiple counts with one SQL query?

    【讨论】:

    • 那么我该如何处理原始查询$sql = "SELECT * FROM users"; 呢?我想显示原始查询的结果(作为一个整体与名称等)以及$row['count_female']$row['count_female'] 我应该只连接这两个查询吗?
    • 您可以通过添加*column name 来查询添加用户信息例如:select *, count(*) as count_all, sum(case when sex = 'female ' then 1 else 0 end) AS count_female, sum(case when sex = 'male ' then 1 else 0 end) AS count_male FROM users 您将获得所有用户信息并按每行计算列,也可以使用fetchAll()而不是fetch() 来获取所有行而不是一行,是的,每行中都会有不必要的计数列重复,但是除了这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多