对于一个表的每个值,获取并显示来自另一个表的相应值的计数

时间:2016-03-21 19:35:04

标签: php mysql

不幸的是我必须在mysql / php中执行此操作。我找了三天,对此有一万条解释但是没有(我再说一遍)对我有用。我试了一下。我不得不问,抱歉。

我有两张桌子 - 文章和控件。

table "articles"
    ------------------
    art_id  | name |
    ------------------
       1    | aaa  |
       2    | bbb  |
       3    | ccc  |
       4    | ddd  |

table "control"
    --------------------------------------------
    con_id | art_id  |      data      |
    --------------------------------------------
       1   |     1   |   something-a  |
       2   |     2   |   something-b  |
       3   |     1   |   something-a  |
       4   |     2   |   something-c  | 
       5   |     3   |   something-f  |

art_id存在于两个表中。现在我想要的 - 查询: "select * from articles order by art_id ASC"显示在表格中 还有一个单元格显示表格CONTROL中每个art_id的计数... 所以我尝试加入,左连接,内连接 - 我得到错误...我也试过每个只获得一个结果(例如2为一切)...这是半右,但它显示正确结果的数组它连加入都没有!!! :

$query = "SELECT art_id, count(*) as counting 
FROM control GROUP BY art_id ORDER BY con_id ASC"; 
$result = mysql_query($query); 
while($row=mysql_fetch_array($result)) {
echo $row['counting'];
}

显示221 -

-------------------------------------------------
art_id | name | count (this one from control) |
-------------------------------------------------
   1   |  aaa |   221   |
   2   |  bbb |   221   |
   3   |  ccc |   221   |

应该是:

for art_id(value1)=2, 
for art_id(2)=2, 
for art_id(3)=1 

它应该很简单 - 就像在查询中显示的关于“文章”表的CONTROL表中的值计数...

表格文章的页面结果查询应该是: "select * from articles order by art_id ASC"

-------------------------------------------------
art_id | name | count (this one from control) |
-------------------------------------------------
   1   |  aaa |   2   |
   2   |  bbb |   2   |
   3   |  ccc |   1   |

所以也许我应该选择JOIN或加入加号...也尝试过,但后来我不确定回应什么是正确的...总而言之我完全迷失了这里。请帮忙。谢谢。

2 个答案:

答案 0 :(得分:1)

想象一下这有两个步骤:

  1. art_id
  2. 获取每control的计数
  3. 使用您的articles表,从第1步
  4. 中获取计数

    这将为您提供如下所示的查询:

    SELECT a.art_id, a.name, b.control_count
    FROM articles a
    INNER JOIN
    (
        SELECT art_id, COUNT(*) AS control_count
        FROM control
        GROUP BY art_id
    ) b
    ON a.art_id = b.art_id;
    

    这将为您提供您正在寻找的结果。

    但是,您可以一次性完成所有操作,而不是使用子查询:

    SELECT a.art_id, a.name, COUNT(b.art_id) AS control_count
    FROM articles a
    INNER JOIN control b
      ON a.art_id = b.art_id
    GROUP BY a.art_id, a.name;
    

    SQL Fiddle demo

答案 1 :(得分:0)

SELECT *, (SELECT COUNT(control.con_id) FROM control WHERE control.art_id = articles.art_id) AS count_from_con FROM articles ORDER BY art_id DESC;

如果我理解你的问题是正确的,那么这个查询应该可以解决问题。

修改:创建您描述的表格,并且它可以正常运行。

SELECT * FROM articles;
+--------+------+
| art_id | name |
+--------+------+
|      1 | aaa  |
|      2 | bbb  |
|      3 | ccc  |
|      4 | ddd  |
+--------+------+
4 rows in set (0.00 sec)

SELECT * FROM control;
+--------+--------+------+
| con_id | art_id | data |
+--------+--------+------+
|      1 |      1 | NULL |
|      2 |      2 | NULL |
|      3 |      1 | NULL |
|      4 |      2 | NULL |
|      5 |      3 | NULL |
+--------+--------+------+
5 rows in set (0.00 sec)

SELECT *, (SELECT COUNT(control.con_id) FROM control WHERE control.art_id = articles.art_id) AS count_from_con FROM articles ORDER BY art_id ASC;
+--------+------+----------------+
| art_id | name | count_from_con |
+--------+------+----------------+
|      1 | aaa  |              2 |
|      2 | bbb  |              2 |
|      3 | ccc  |              1 |
|      4 | ddd  |              0 |
+--------+------+----------------+

您还没有完全解释打印输出的内容,但以下是PHP中的示例:(使用PDO代替mysql_

$pdo = new PDO(); // Make your connection here
$stm = $pdo->query('SELECT *, (SELECT COUNT(control.con_id) FROM control WHERE control.art_id = articles.art_id) AS count_from_con FROM articles ORDER BY art_id ASC');
while( $row = $stm->fetch(PDO::FETCH_ASSOC) )
{
    echo "Article with id: ".$row['art_id']. " has " .$row['count_from_con'].' connected rows in control.';
}

或者使用mysql_扩展名:

$result = mysql_query('SELECT *, (SELECT COUNT(control.con_id) FROM control WHERE control.art_id = articles.art_id) AS count_from_con FROM articles ORDER BY art_id ASC');
while( $row = mysql_fetch_assoc($result) )
{
    echo "Article with id: ".$row['art_id']. " has " .$row['count_from_con'].' connected rows in control.';
}

这应该是足以帮助您完成所需工作的示例。