如何将这个巨大的分割查询转换为单个查询

时间:2010-09-13 07:13:25

标签: php mysql

我在一个页面中从不同的表中获取不同的查询。我想减少数据库跳闸。

这是我的代码..

$query = "SELECT COUNT(*) as cnt_news FROM news";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT COUNT(*) as cnt_adv FROM advertisements";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$query = "SELECT COUNT(*) as cnt_comm FROM comments";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT SUM(CASE WHEN c.approve = '1' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_approved,
          SUM(CASE WHEN c.approve = '0' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_unapproved,
          SUM(CASE WHEN c.spam = '1' THEN 1 ELSE 0 END) AS cnt_spam
          FROM COMMENTS c";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT SUM(a.amount) as t_amnt,
          SUM(a.cashpaid) as t_cpaid,
          SUM(a.balance) as t_bal
          FROM advertisements a";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

我对如何使查询从三个不同的表,新闻,广告和评论中选择值感到困惑。我如何削减我的代码和数据库之旅?

谢谢。

3 个答案:

答案 0 :(得分:1)

您不必削减数据库行程,但可以使用函数

来剪切代码

制作通用功能会更好。

<?php
$num_news = dbgetvar("SELECT COUNT(*) FROM news");
$num_adv  = dbgetvar("SELECT COUNT(*) FROM advertisements");
$num_comm = dbgetvar("SELECT COUNT(*) FROM comments");

function dbgetvar($query){
  $res = mysql_query($query);
  if (!$res) {
    trigger_error("dbget: ".mysql_error()." in ".$query);
    return FALSE;
  }
  $row = mysql_fetch_row($res);
  if (!$row) return NULL;
  return $row[0];
}

总是从重复的代码中创建一个函数。

答案 1 :(得分:1)

$query = "SELECT
COUNT(n.*) as cnt_news,
COUNT(a.*) as cnt_adv,
COUNT(c.*) as cnt_comm,
SUM(CASE WHEN c.approve = '1' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_approved,
    SUM(CASE WHEN c.approve = '0' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_unapproved,
    SUM(CASE WHEN c.spam = '1' THEN 1 ELSE 0 END) AS cnt_spam,
SUM(a.amount) as t_amnt,
    SUM(a.cashpaid) as t_cpaid,
    SUM(a.balance) as t_bal
FROM
news n, advertisements a, comments c";

这应该在一个查询中完成。

答案 2 :(得分:1)

您可以转而使用mysqli库而不是mysql库来连接MySQL。使用mysqli::multi_query一次运行多个查询并获得结果。

几乎所有mysql_函数都可用作mysqli_ functions:

<?php
$link = mysqli_connect("localhost", "user", "password", "dbname");

/* check connection */
if (mysqli_connect_errno())
{
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//these are your queries. Put them all in a string separated by semicolon ;
$query  = "SELECT COUNT(*) as cnt_news FROM news;";
$query .= "SELECT COUNT(*) as cnt_adv FROM advertisements;";
$query .= "SELECT COUNT(*) as cnt_comm FROM comments;";

// Now execute multi query
if (mysqli_multi_query($link, $query))
{
    do {
        /* store first result set */
        if ($result = mysqli_store_result($link))
        {
            while ($row = mysqli_fetch_row($result))
            {
                printf("%s\n", $row[0]);
            }
            mysqli_free_result($result);
        }
        /* print divider */
        if (mysqli_more_results($link)) {
            printf("-----------------\n");
        }
    } while (mysqli_next_result($link));
}

/* close connection */
mysqli_close($link);
?>

我只展示了三个查询的示例。您可以在$query中添加更多查询。这样就会有对MySQL的单一请求。