Group By 2列具有相似的信息以与第二个表链接

时间:2013-08-28 18:26:25

标签: php mysql group-by

我有两列,信息类似:

第1栏=第1项,第3项,第5项 第3栏=第3项,第5项,第8项

我想显示两个列中每个项目的总数。

我有这个:


$sql = mysql_query("SELECT FirstTypeID, SecondTypeID, ThirdTypeID, DesignID, COUNT(DesignID) FROM designs WHERE Approved = '1' GROUP BY FirstTypeID, SecondTypeID, ThirdTypeID");
while ($row = mysql_fetch_array($sql)) {
    $DesignID = stripslashes($row['DesignID']);
    $FirstTypeID = stripslashes($row['FirstTypeID']);
    $SecondTypeID = stripslashes($row['SecondTypeID']);
    $ThirdTypeID = stripslashes($row['ThirdTypeID']);
    $Total = stripslashes($row['COUNT(DesignID)']);
}

$result2 = mysql_query("SELECT * FROM types WHERE TypeID = '$FirstTypeID' OR TypeID = '$SecondTypeID' OR TypeID = '$ThirdTypeID'");
while ($row2 = mysql_fetch_array($result2)) {
    echo "<li><a href='index_type.php?TypeID=".$row2{'TypeID'}."'>".$row2{'TypeName'}." (" . $Total . ")</a></li>";
}

但是我没有得到我想要的结果,它只给了我一栏的结果。

1 个答案:

答案 0 :(得分:1)

我不确定这是否是你所要求的,但在这里它是

$typesIDs = array(type0 => "", type1 => "", type2 => ""...);

foreach($typesID as $index=>$value){
   $query = "SELECT COUNT(*) AS total FROM designs COLUMN1 = ".$index." OR COLUMN2 = ".$index;
   $sql = mysql_query($query);
   $row = mysql_fetch_array($sql);
   array[$index] => $row["total"]
}