How to multiple query results in order to reduce the query number?

六眼飞鱼酱① 提交于 2021-02-04 11:36:08

问题


I want to list comments from my database depending on their type.

There are three types of comments in my database and I call them with three different queries.

//01 - Awaiting Comments
  $query = $handler->prepare("SELECT * FROM comments WHERE confirmed = 0");

  $query->execute();
  $r = $query->fetchAll(PDO::FETCH_ASSOC);
    echo "<h1>Awaiting Comments</h1>";
    foreach($r as $r_)  {
    echo "<li>r_[title]</li>";
  }
//02 - Comments waiting for confirmation
  $query = $handler->prepare("SELECT * FROM comments WHERE confirmed = 2");

  $query->execute();
  $r = $query->fetchAll(PDO::FETCH_ASSOC);
    echo "<h1>Comments waiting for confirmation</h1>";
    foreach($r as $r_)  {
    echo "<li>r_[title]</li>";
  }

//03 - Confirmed comments
  $query = $handler->prepare("SELECT * FROM comments WHERE confirmed = 1");

  $query->execute();
  $r = $query->fetchAll(PDO::FETCH_ASSOC);
    echo "<h1>Confirmed Comments</h1>";
    foreach($r as $r_)  {
    echo "<li>r_[title]</li>";
  }  

With my current code i get the output i want like that:

Awaiting Comments
-comment 1
-comment 8
-comment 5

Comments waiting confirmation
-comment 9
-comment 4
-comment 2

Confirmed Comments
-comment 3
-comment 6
-comment 7

Is there any way to get same output with single query instead of three of them?


回答1:


PDO is a bit more than everyone thinks it is. For example, it has a magnificent feature for you, called PDO::FETCH_GROUP.

Not to mention other little improvements that can make your code dramatically shorter.

$r = $handler->query("SELECT confirmed, c.* FROM comments c")->fetchAll(PDO::FETCH_GROUP);

Is all the code you need.

here you are selecting the confirmed field first and then tell PDO to group (or "multiply") the results based on its value.

And now you can print your comments wherever you want

// Awaiting Comments
foreach($r[0] as $r_) {
    echo "<li>$r_[title]</li>";
}

// Confirmed comments
foreach($r[2] as $r_) {
    echo "<li>$r_[title]</li>";
}

Or, to make it in one loop

$titles = [
    0 => 'Awaiting Comments',
    2 => 'Comments waiting confirmation',
    1 => 'Confirmed Comments',
];

foreach ($titles as $code => $title)
{
    echo "<h3>$title</h3>";
    foreach($r[$code] as $r_) {
        echo "<li>$r_[title]</li>";
    }
}



回答2:


You can use this query syntax:

SELECT * FROM comments 
WHERE (confirmed >= 0 AND confirmed < 3 ) 
ORDER BY FIELD(confirmed,0,2,1)

In this way you have all rows sorted by your desired output.

If the confirmed field allows only 0,1,2 values, you can sempliy in this way:

SELECT * FROM comments 
ORDER BY FIELD(confirmed,0,2,1)

Then:

while( $row = $query->fetch(PDO::FETCH_ASSOC) )
{
    if    ( $row['confirmed'] == 0 )
    {
        (...)
    }
    elseif( $row['confirmed'] == 1 )
    {
        (...)
    }
    else
    {
        (...)
    }
}



回答3:


Yes, you can use and IN condition:

SELECT * FROM comments WHERE confirmed IN (0, 1, 2)

Then you can loop through the results as many times as you like to create the output.




回答4:


You can do: SELECT * FROM comments WHERE confirmed=1 OR confirmed=2 OR confirmed=0 Or use IN



来源:https://stackoverflow.com/questions/35317555/how-to-multiple-query-results-in-order-to-reduce-the-query-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!