【发布时间】:2013-02-19 04:51:39
【问题描述】:
我需要从以下格式的两个表中打印摘要:
Product | Grand Total
--------+---------
Book | 8000
Pen | 5000
Ruler | 0
table_product
id | name
-----+---------
1 | Book
2 | Pen
3 | Ruler
table_transaction
id | cashier | product | total
-----+---------+---------+---------
1 | john | 1 | 5000
2 | doe | 1 | 3000
3 | john | 2 | 2000
4 | other | 2 | 3000
这可以只用 1 个查询来完成吗?
编辑: 之前,我在 table_transaction 上使用了这个查询:
$this->db->select('product');
$this->db->select('total');
$this->db->from('table_transaction');
$this->db->select_sum('total', 'grand_total');
$this->db->group_by('product');
$query = $this->db->get();
但它没有显示尚未在表格中的产品。 即使还没有交易,我也想打印所有产品。
【问题讨论】:
-
很简单的加入。查找用于连接表的 mysql 文档。
标签: php mysql codeigniter activerecord