2
我有3个表,LEFT JOIN在笨
itemmaster
|--------|----------|
| id | name |
|--------|----------|
| 1 | Pizza |
|--------|----------|
| 2 | Burger |
|--------|----------|
| 3 | Pepsi |
---------------------
order
|--------|----------|
|orderid | date |
|--------|----------|
| 1 | 1-1-11 |
|--------|----------|
| 2 | 2-1-11 |
|--------|----------|
| 3 | 3-1-11 |
---------------------
orderdetails
|--------|-------------|---------|---------|
| id | orderid |itemid |quantity |
|--------|-------------|---------|---------|
| 1 | 1 | 1 | 10 |
|--------|-------------|---------|---------|
| 2 | 1 | 2 | 20 |
|--------|-------------|---------|---------|
| 3 | 2 | 1 | 10 |
-------------------------------------------
我想加入这3个表来获取放置在特定日期的订单项的数量。 我已经试过是
$this->db->from('itemmaster');
$this->db->join('orderdetails', 'orderdetails.itemid= itemmaster.id','left');
$this->db->join('order', 'order.orderid= orderdetails.orderid');
$this->db->where('order.date',"1-1-11");
$query = $this->db->get();
我得到的结果,
Pizza------ 10
Burger------10
我要的是,
Pizza-------10
Burger------20
Pepsi-------0
尽量让你的第二个加入左连接。直接在SQL中,id尝试使用coalesce(用某种东西替换空值)。例如coalesce(number of Burger,0)。如果伯爵数为空,那么youd在查询结果中看到0 – Alex 2014-10-07 16:00:28
@Alex:感谢您的帮助。我已经尝试了第二次加入,但我仍然只得到1项。 – 2014-10-07 16:13:43
如果我们一步一步做一次一次加入,你总会得到1个结果吗? – Alex 2014-10-07 16:18:11