【发布时间】:2014-10-11 18:29:41
【问题描述】:
虽然我可以让连接工作,但它不会返回可能的最低交易。结果应该是最低的手机价格和最低的每月成本。目前我可以得到最低的手机价格,但不是最低的月费,同样最低的月费,但不是最低价的手机。
我有 3 张桌子 DEALS、TARIFFS 和 HANDSETS。当客户访问制造商产品列表页面时,它会加载与该制造商相关的所有产品(在大多数情况下大约 40 种产品)。我想要的是为每种产品加载最便宜的交易。关税表有超过 4000 条记录。手机表有超过 3000 条记录。 Deals 表有超过 100 万条记录。
下面的 3 个表格带有相关列:
Handset Table
==================================
id | manufacturer_id
-----------------------------------
100 1
Deals Table
==================================================
tariff_id | handset_id | handset_cost
--------------------------------------------------
15 100 44.99
20 100 114.99
Tariffs Table
==============================
id | monthly_cost
------------------------------
15 12.50
20 7.50
这是查询
$this->db->select('h.name, t.monthly_cost, d.handset_cost');
$this->db->from('aff_deals d');
$this->db->join('aff_tariffs t', 't.id = d.tariff_id', 'inner');
$this->db->join('aff_handsets h', 'h.id = d.handset_id', 'inner');
if (isset($manuid)) {
$this->db->where('h.manufacturer_id', $manuid);
}
$this->db->order_by('d.handset_cost ASC');
$this->db->order_by('t.monthly_cost ASC');
$this->db->group_by('h.name');
$query = $this->db->get();
$result = $query->result();
不幸的是,当我需要 44.99 英镑和 12.50 英镑时,这会返回 114.99 英镑的手机成本和 7.50 英镑的月度成本。上面的示例是一个简单的快照。我尝试了 MIN(d.handset_cost),子查询,但无法获得所需的结果。不知何故,我需要能够获得最低价格的手机,即使它是 0.00(免费),每月的成本也是如此。任何帮助都将受到欢迎。
【问题讨论】:
标签: mysql codeigniter join