MySQL:使用PHP显示基于2列的记录数

时间:2011-11-15 15:52:31

标签: php mysql

我有以下专栏..

Name, pageID, promoID

Bob, 123, promo1
Steve, 123, promo1
Nancy, 123, promo2
Lyn, 123, promo2
Amy, 123, promo2
Jeff, 123, promo2
Joe, 456, promo3
Bill, 456, promo3
Joe, 456, promo3

我想创建一个仅包含pageID为123的记录的报告,然后按promoID对它们进行分组。所以我的表输出看起来像这样..

Records for pageID 123..
promo1 = 2 entries
promo2 = 4 entries

什么查询可以实现这个目标?

以下是我目前在HTML表格中吐出的内容。

$querysubmissions = mysql_query("SELECT promoID FROM submissions WHERE fbPageID = '123'");
$numbersubmissions = mysql_num_rows($querysubmissions); 


$query = mysql_query("SELECT * FROM submissions WHERE fbPageID = '$page_id' GROUP BY promoTitle");

while($data = mysql_fetch_assoc($query)){
    $title = $data['promoTitle'];
    echo '<tr>';
    echo '<td>'.$title.'</td>';
    echo '<td>'.$numbersubmissions.'</td>'; 
    echo "<td><a href='submission-csv.php?promoid=".$data['promoID']."'>Export</a></td>";
    echo '</tr>';
    }

    mysql_close();

但这只返回123 pageID和所有promoID的提交总数。我将如何在PHP中回应这个?

4 个答案:

答案 0 :(得分:5)

在您的选择中尝试GROUP BY并使用count(*)

SELECT promoID, count(*) as `count`
FROM submissions 
WHERE fbPageID = '123'
GROUP BY promoID;

答案 1 :(得分:0)

$querysubmissions = mysql_query("SELECT promoID,COUNT(promoID) as count FROM submissions WHERE fbPageID = '123' GROUP BY promoID");
$numbersubmissions = mysql_num_rows($querysubmissions); 

答案 2 :(得分:0)

你可以在sql查询中完成所有这些。

Select PromoID, Count(PromoID) as pCount From submissions where fbPageID = '123' Group By PromoID

您也可以为多个页面执行此操作

Select concat('Page ',fbPageID,', ',PromoID,' = ',Count(PromoID)) as result From submissions Group By fbPageID, PromoID

这有额外的好处,不需要为每个页面做一个选择。所以你的结果看起来像

Page 123, Promo1 = 2
Page 123, Promo2 = 4
Page 456, Promo3 = 3

答案 3 :(得分:0)

该查询将以'promo1 = 2条目'格式显示

SELECT 'Records for pageID 123' ResultDisplay
UNION
SELECT
    CONCAT(promoID,' = ',cnt,' entr',IF(cnt=1,'y','ies'))
FROM
    (SELECT promoID,count(1) cnt 
    FROM submissions  
    WHERE pageID = '123' 
    GROUP BY promoID) A; 

以下是您的示例数据加载到提交表中:

mysql> use junk
Database changed
mysql> drop table if exists submissions;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> create table submissions
    -> (name varchar(10),pageID int,promoID varchar(10)) ENGINE=MyISAM;
Query OK, 0 rows affected (0.08 sec)

mysql> insert into submissions values
    -> ('Bob'  , 123 , 'promo1'),
    -> ('Steve', 123 , 'promo1'),
    -> ('Nancy', 123 , 'promo2'),
    -> ('Lyn'  , 123 , 'promo2'),
    -> ('Amy'  , 123 , 'promo2'),
    -> ('Jeff' , 123 , 'promo2'),
    -> ('Joe'  , 456 , 'promo3'),
    -> ('Bill' , 456 , 'promo3'),
    -> ('Joe'  , 456 , 'promo3');
select * from submissions;Query OK, 9 rows affected (0.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> select * from submissions;
+-------+--------+---------+
| name  | pageID | promoID |
+-------+--------+---------+
| Bob   |    123 | promo1  |
| Steve |    123 | promo1  |
| Nancy |    123 | promo2  |
| Lyn   |    123 | promo2  |
| Amy   |    123 | promo2  |
| Jeff  |    123 | promo2  |
| Joe   |    456 | promo3  |
| Bill  |    456 | promo3  |
| Joe   |    456 | promo3  |
+-------+--------+---------+
9 rows in set (0.00 sec)

以下是查询的执行:

mysql> SELECT 'Records for pageID 123' ResultDisplay
    -> UNION
    -> SELECT
    ->     CONCAT(promoID,' = ',cnt,' entr',IF(cnt=1,'y','ies'))
    -> FROM
    ->     (SELECT promoID,count(1) cnt
    ->     FROM submissions
    ->     WHERE PageID = '123'
    ->     GROUP BY promoID) A
    -> ;
+------------------------+
| ResultDisplay          |
+------------------------+
| Records for pageID 123 |
| promo1 = 2 entries     |
| promo2 = 4 entries     |
+------------------------+
3 rows in set (0.03 sec)

试一试!!!

这是PHP中的

$givenpageID = 123;
$sql = "SELECT 'Records for pageID " . $givenpageID . "' ResultDisplay UNION SELECT CONCAT(promoID,' = ',cnt,' entr',IF(cnt=1,'y','ies')) FROM (SELECT promoID,count(1) cnt FROM submissions WHERE PageID = '" . $givenpageID . "' GROUP BY promoID) A";
$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}
while ($row = mysql_fetch_assoc($result)) {
    echo $row["ResultDisplay"];
}
mysql_free_result($result);