简体   繁体   中英

php code to count how many time a number appear in a row using INNER JOIN statement

Am trying to count how many time a number appear in a row(in order to calculate number of sale per person ) i have these table in my database books , shelf , here is my code

$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>';


      }
  }


?>

so am looking for a way to count the a number appear in the shelf table in bookid as row, for instance if 3 appears 7 time in the shelf table than it should calculate it and display it as number of sales=7

here is my shelf table structure 这是我的架子桌子

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

you probably don't need a JOIN just

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM