好的,我已经阅读了所有重复的问题,但我的查询与其他问题略有不同 这是我的数据库表:
category
cat_id | cat_name | parent | parent_id
-----------------------------------------
29 | Apparels | -- | 0
-----------------------------------------
33 | Mens | Apparels| 29
Products
id | product_name | category |
--------------------------------
28 | Tee | 33
--------------------------------
29 | Jeans | 33
所以我想要每个类别的产品数量。我不知道自己哪里出错了。
这是我的PHP代码:
if (!empty($_REQUEST['term']))
{
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM category WHERE cat_name LIKE '%" . $term . "%' or parent LIKE '%" . $term . "' or cat_status LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1)
{
$dynamicList="";
while ($row = mysql_fetch_array($r_query))
{
// $cat_id=;
/*$dynamicList .= '
<img style="border:#666 1px solid;" src="../storeadmin/category/thumbs/' . $row['cat_id'] . '.jpg" width="77" />';*/
$catid=$row['cat_id'];
$result1=mysql_query("SELECT count(1) From products where category='$catid'");
echo "SELECT count(1) From products where category='$catid'";
$rows=mysql_fetch_array($result1);
echo $rows['product_name'];
echo "<tr>";
echo "<td width='10%'>" . $row['cat_id'] . "</td>";?>
<td width="10%"><img style='border:#666 1px solid;' width='70' src="<?php echo $row["cat_image"]; ?>" alt="" /></td>
<?php
//echo "<td><img style='border:#666 1px solid;' width='70' src='category/thumbs/". $row['cat_id'].".jpg' /></td>";
//echo "<td>".$dynamicList."</td>";
echo "<td width='10%'>" . $row['cat_name'] . "</td>";
echo "<td width='10%'>" . $row['parent'] . "</td>";
echo "<td width='10%'>" . $row['cat_status'] . "</td>";
}
答案 0 :(得分:1)
unsigned
也许这就是错误。虽然 mysql已被弃用,但我不建议您继续使用。
答案 1 :(得分:0)
答案 2 :(得分:0)
选择category.cat_name,将count(*)选为产品类别,Products where 类别。 cat_id = Products.category group by category.cat_name