php代码,用于计算使用INNER JOIN语句在一行中出现的次数

时间:2017-10-30 12:31:03

标签: php mysql

我试图计算一个数字连续出现的次数(为了计算每人的销售数量)我在我的数据库booksshelf中有这些表,这是我的代码

$UBooks = $dbhandle->query("SELECT *  FROM books 
    INNER JOIN shelf ON books.id = shelf.bookid WHERE books.author_id = '$id';
    ");

    if ($UBooks->num_rows > 0)
    {
      while($row = $UBooks->fetch_assoc()) 
      {

        $ruid = $row["bid"];
          $title = $row["title"];
          $description = $row["description"];
          $bookurl = "../../".$row["bookurl"];
          $publish = $row["publish"];
          $price = $row["price"];
          $Fullname = $row["bookid"];



          if($publish == 1)
          {
            $publish = '<input type="checkbox" checked bid="'.$ruid.'" title="Click to Un-Publish Book" onchange="activateBook(this);" />';
          }
          else if ($publish == 0)
          {
            $publish = '<input type="checkbox" bid="'.$ruid.'" title="Click to Publish Book" onchange="activateBook(this);" />';
          }
          else if ($publish == 3)
          {
            $publish = '<input type="checkbox" bid="'.$ruid.'" title="Click to Publish Book" onchange="activateBook(this);" /> <label>For Review</label>';
          }
        $BooksTab .= '<tr>
                           <td>'.$title.'</td>  <td>'.$description.'</td> <td>'.$price.'</td>
                          <td>'.$Fullname.'</td>
                        </tr>';


      }
  }


?>

所以我正在寻找一种方法来计算bookid中货架表中出现的一个数字作为行,例如,如果3在货架表中出现3次而不是计算它并将其显示为数量销售= 7

这是我的货架表结构this is my shelf table

为什么这个是我的书桌sturture why this is my book table

1 个答案:

答案 0 :(得分:0)

你可能不需要加入

 SELECT bookid, count(*)
 FROM shelf
 GROUP BY bookid