如何计算具体价值?

时间:2022-10-14 15:42:51

I use following code:

我使用以下代码:

$query = "SELECT ves, COUNT(ves) FROM books GROUP BY ves"; 
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "There are ". $row['COUNT(ves)'] ." books";
echo "<br />";
}

I gives me following result:

我给了我以下结果:

There are 176 books. There are 5 books.

有176本书。有5本书。

First line result is total books on db. I need to get only second result that correspond to the value for ves colum, How can I get this?

第一行的结果是db上的总书。我只需要获得与ves colum值相对应的第二个结果,我该如何得到它?

Thank you in advance. NPinelo

先谢谢你。 NPinelo

3 个解决方案

#1


0  

Use a var (in this case $i) to control when you are in the second row

使用var(在本例中为$ i)来控制何时在第二行

    $query = "SELECT ves, COUNT(ves) FROM books GROUP BY ves"; 
    $result = mysql_query($query) or die(mysql_error());
    // Print out result

$i = 1;

    while($row = mysql_fetch_array($result)){

if($i == 2) {

    echo "There are ". $row['COUNT(ves)'] ." books";
    echo "<br />";

}

$i++;
    }

#2


0  

Try this example:

试试这个例子:

$query = "SELECT ves, COUNT(ves) as ves_count FROM books GROUP BY ves"; 
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "There are ". $row['ves_count'] ." books";
echo "<br />";
}
  1. here at first you have to make an alias for getting counted values
  2. 首先,您必须为获取计数值创建别名

  3. and secondly use $row['ves_count'] instead of $row['COUNT(ves)']
  4. 其次使用$ row ['ves_count']而不是$ row ['COUNT(ves)']

#3


0  

Thanks to all helpfull minds, following code was what finally working as I need:

感谢所有有用的思想,下面的代码是我最终需要的工作:

$query = "SELECT ven, COUNT(ven) FROM books WHERE ven='en'"; 
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "Here are ". $row['COUNT(ven)'] ." books";
echo "<br />";
}

#1


0  

Use a var (in this case $i) to control when you are in the second row

使用var(在本例中为$ i)来控制何时在第二行

    $query = "SELECT ves, COUNT(ves) FROM books GROUP BY ves"; 
    $result = mysql_query($query) or die(mysql_error());
    // Print out result

$i = 1;

    while($row = mysql_fetch_array($result)){

if($i == 2) {

    echo "There are ". $row['COUNT(ves)'] ." books";
    echo "<br />";

}

$i++;
    }

#2


0  

Try this example:

试试这个例子:

$query = "SELECT ves, COUNT(ves) as ves_count FROM books GROUP BY ves"; 
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "There are ". $row['ves_count'] ." books";
echo "<br />";
}
  1. here at first you have to make an alias for getting counted values
  2. 首先,您必须为获取计数值创建别名

  3. and secondly use $row['ves_count'] instead of $row['COUNT(ves)']
  4. 其次使用$ row ['ves_count']而不是$ row ['COUNT(ves)']

#3


0  

Thanks to all helpfull minds, following code was what finally working as I need:

感谢所有有用的思想,下面的代码是我最终需要的工作:

$query = "SELECT ven, COUNT(ven) FROM books WHERE ven='en'"; 
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "Here are ". $row['COUNT(ven)'] ." books";
echo "<br />";
}