我有2個表格,'部門'和'文件'。如何計算具有相同列數據的行數並顯示到表中?
表department
| doc_id | dept_name |
----------------------------------
| 1 | Information Technology|
| 2 | Software Development |
| 3 | Human Resource |
| 4 | Accounting |
| 5 | Support |
表document
| doc_id | doc_name | author | description | department |
----------------------------------------------------------------------------
| 1 | Maps | User1 | sample | Information Technology |
| 2 | Audits | User3 | sample | Software Development |
| 3 | Image | User1 | sample | Information Technology |
| 4 | Papers | User4 | sample | Human Resource |
| 5 | Print Screen| User1 | sample | Software Development |
| 6 | Transaction | User3 | sample | Accounting |
| 7 | Graph | User1 | sample | Support |
| 8 | Excel | User1 | sample | Information Technology |
現在,我想有兩列顯示錶:部門和total_doc。
輸出:
| department |total_doc|
-----------------------------------
| Information Technology| 3 |
| Software Development | 2 |
| Human Resource | 1 |
| Accounting | 1 |
| Support | 1 |
我想要顯示的部門內的總文檔並以升序排列。
這是我的查詢。(不知道)
SELECT department, count(doc_name) as 'total_doc' FROM tbl_document GROUP BY doc_name
我使用MVC模式的笨。
$this->db->select("department, count(doc_name) as 'total_doc'");
$this->db->from('document');
$this->db->group_by('doc_name');
此外,我怎樣才能在表中顯示這個?喜歡在HTML中使用foreach?
你能送我.sql文件,這樣我可以通過做測試爲您提供在我的系統中。 – 2014-11-03 09:09:14
我不能!我只是在我們的組織中維護一個程序,我的一些數據是保密的。 – jned29 2014-11-03 09:12:56