选择table_link上的count作为多对多关系

时间:2016-04-04 08:26:24

标签: mysql

我有两个表与许多关系manu这样的表

第一名:表格请求

MariaDB [ittresnamuda]> select ID_REQUEST, NUMBER_OF_LETTER, TANGGAL_TERIMA FROM tb_requestfix limit 10;
+------------+------------------+---------------------+
| ID_REQUEST | NUMBER_OF_LETTER | TANGGAL_TERIMA      |
+------------+------------------+---------------------+
|         32 |              001 | 2016-01-02 09:00:00 |
|         33 |              002 | 2016-01-02 09:00:00 |
|         34 |              003 | 2016-01-02 13:00:00 |
|         47 |              004 | 2016-01-04 09:00:00 |
|         48 |              005 | 2016-01-04 10:49:00 |
|         52 |              006 | 2016-01-04 11:14:00 |
|         53 |              007 | 2016-01-04 13:50:00 |
|         54 |              008 | 2016-01-04 16:17:00 |
|         55 |              009 | 2016-01-04 16:28:00 |
|         56 |              010 | 2016-01-05 13:12:00 |
+------------+------------------+---------------------+
10 rows in set (0.00 sec)

第二。表项

MariaDB [ittresnamuda]> select * from tb_item_request ;
+---------+-----------+
| ID_ITEM | NAMA_ITEM |
+---------+-----------+
|       1 | Login     |
|       2 | Printer   |
|       3 | Monitor   |
|       4 | Computer  |
|       5 | Network   |
|       6 | Lain-lain |
+---------+-----------+
6 rows in set (0.00 sec)

因此,我创建了一个帮助表来将它们管理成一对多的关系。这是表格。

表格链接

MariaDB [ittresnamuda]> select * from tb_link_item_request LIMIT 10;
+-----+------------+---------+
| ID  | id_request | id_item |
+-----+------------+---------+
| 125 |         34 |       6 |
| 146 |         32 |       4 |
| 147 |         33 |       1 |
| 148 |         47 |       4 |
| 153 |         48 |       6 |
| 154 |         52 |       1 |
| 155 |         53 |       2 |
| 156 |         53 |       6 |
| 157 |         54 |       4 |
| 158 |         55 |       3 |
+-----+------------+---------+
10 rows in set (0.00 sec)

现在,我想基于请求的项目,月份和年份来计算请求记录,如下所示。

+---------+-----------+---------------+
| ID_ITEM | NAMA_ITEM | TOTAL_OF_ITEM |
+---------+-----------+---------------+
|       1 | Login     |       2       |
|       2 | Printer   |       1       |
|       3 | Monitor   |       1       |
|       4 | Computer  |       3       |
|       5 | Network   |       0       |
|       6 | Lain-lain |       3       |
+---------+-----------+---------------+

到目前为止,我试过这样,

MariaDB [ittresnamuda]> SELECT b.ID_ITEM, b.NAMA_ITEM, COUNT(a.id_item) AS TOTAL_ITEM
-> FROM tb_link_item_request a
->
-> INNER JOIN tb_item_request b
-> ON a.id_item = b.ID_ITEM
->
-> INNER JOIN tb_requestfix c
-> ON a.id_request = c.ID_REQUEST
->
-> WHERE a.id_item = 1 AND MONTH(c.TANGGAL_TERIMA) = 01 AND YEAR(c.TANGGAL_TERIMA) = 2016;

+---------+-----------+------------+
| ID_ITEM | NAMA_ITEM | TOTAL_ITEM |
+---------+-----------+------------+
|       1 | LOGIN     |          2 |
+---------+-----------+------------+
1 row in set (0.00 sec)

任何帮助它如此欣赏,

4 个答案:

答案 0 :(得分:1)

您只需要添加group by子句并删除where子句:

SELECT b.ID_ITEM, b.NAMA_ITEM, YEAR(c.TANGGAL_TERIMA), MONTH(c.TANGGAL_TERIMA), COUNT(a.id_item) AS TOTAL_ITEM
FROM tb_link_item_request a
INNER JOIN tb_item_request b ON a.id_item = b.ID_ITEM
INNER JOIN tb_requestfix c ON a.id_request = c.ID_REQUEST
GROUP BY b.id_item, b.NAMA_ITEM, MONTH(c.TANGGAL_TERIMA) YEAR(c.TANGGAL_TERIMA)

答案 1 :(得分:1)

试试这个:

SELECT b.ID_ITEM, b.NAMA_ITEM, COALESCE(COUNT(a.id_item),0) AS TOTAL_ITEM
   FROM tb_item_request b
   LEFT JOIN tb_link_item_request a ON a.id_item = b.ID_ITEM
   LEFT JOIN tb_requestfix c ON a.id_request = c.ID_REQUEST
  WHERE COALESCE(MONTH(c.TANGGAL_TERIMA),-1) = 01 AND COALESCE(YEAR(c.TANGGAL_TERIMA),-1) = 2016
  GROUP BY b.ID_ITEM, b.NAMA_ITEM;

答案 2 :(得分:0)

试试这个:

SELECT b.ID_ITEM, b.NAMA_ITEM, COUNT(a.id_item) AS TOTAL_ITEM
 FROM tb_item_request b
LEFT JOIN tb_link_item_request a ON a.id_item = b.ID_ITEM
LEFT JOIN tb_requestfix c ON a.id_request = c.ID_REQUEST AND MONTH(c.TANGGAL_TERIMA) = 01 AND YEAR(c.TANGGAL_TERIMA) = 2016
group by b.id_item, b.NAMA_ITEM

答案 3 :(得分:0)

如果您不需要Group By子句,那么这就是

SELECT distinct b.ID_ITEM, b.NAMA_ITEM,
(select COUNT(id_item) from tb_link_item_request a1 where a1.id_item = a.id_item) AS TOTAL_ITEM
FROM tb_item_request b
LEFT JOIN tb_link_item_request a ON a.id_item = b.ID_ITEM
LEFT JOIN tb_requestfix c ON a.id_request = c.ID_REQUEST AND MONTH(c.TANGGAL_TERIMA) = 01 AND YEAR(c.TANGGAL_TERIMA) = 2016