我有下一个mysql查询:
$productn = $jdb->query('SELECT t2.title FROM '.DB_PREFIX.'shopping_cart AS t1 LEFT JOIN '.DB_PREFIX.'shop_order_details AS t2 ON t1.shopid WHERE t1.session = "'.smartsql($_SESSION['shopping_cart']).'"');
while ($rowp = $productn->fetch_assoc()) {
// my product title
$productname = $rowp["title"];
}使用print_r($productname);我得到了类似于:
Name1Name1Name1Name2Name2
我想要显示如下内容:
3个Name1 2个Name2
或
3个Name1、2个Name2
有可能做到这一点吗?
发布于 2012-06-19 17:39:43
好了,我终于能够做出我想要的东西了,所以我想在这里分享它,以备将来使用。
这就是我所做的:
$productn = $jdb->query('SELECT t2.title, count(t2.title) num FROM '.DB_PREFIX.'shop_order_details AS t2 WHERE t2.orderid ="'.$orderid.'" GROUP BY t2.title');
while($registro=$productn->fetch_array()){
echo $registro['title'].' x '.$registro['num']."<br>";
}我在我的类中定义了fetch_array():
function fetch_assoc() {
if($this->result) {
return mysql_fetch_assoc($this->result);
} else {
return false;
}
}感谢大家的帮助。我给每个人都加了分!
发布于 2012-06-19 02:20:57
SQL查询应该是这样的:
select name,count(name) from table group by name ;希望这对你有帮助。
发布于 2012-06-19 02:22:19
您可能希望按标题分组,并计算每个分组出现的次数。
SELECT t2.title, COUNT(t2.title)
FROM shopping_cart AS t1
LEFT JOIN shop_order_details AS t2 ON t1.shopid
WHERE t1.session = <session>
GROUP BY t2.title请看一下COUNT(*)和GROUP BY函数参考。
https://stackoverflow.com/questions/11088665
复制相似问题