为不同的列加入相同的 table 两次
joining the same table twice for different columns
我有一个 table 个产品和一个 table 个位置。 products table 有两列 pickLocation 和 recLocation。在位置 table 我有 id 和 name 列。 pickLocation 和 recLocation 的 id 来自位置 table。我怎样才能在 codeigniter 中加入 table。
这是我的代码
$this->db->select("locations.name as plname");
$this->db->select("locations.name as rcname");
$this->db->join("locations","locations.id=products.pickLocation","LEFT");
$this->db->join("locations","locations.id=products.recLocation","LEFT");
这是产品 table
+----+--------------+-------------+
| Id | pickLocation | recLocation |
+----+--------------+-------------+
| 1 | 12 | 23 |
| 2 | 12 | 12 |
+----+--------------+-------------+
这是位置table
+----+-----------+--+
| Id | name | |
+----+-----------+--+
| 12 | Location1 | |
| 23 | Location2 | |
+----+-----------+--+
我想要这样的结果
+-----------------------+
| 1 Location1 Location2 |
+-----------------------+
| 2 Location1 Location1 |
+-----------------------+
您需要使用别名,以便能够区分这两个联接。尝试这样的事情:
$this->db->select("pickLoc.name as plname");
$this->db->select("recLoc.name as rcname");
$this->db->join("locations as pickLoc","pickLoc.id=products.pickLocation","LEFT");
$this->db->join("locations as recLoc","recLoc.id=products.recLocation","LEFT");
使用别名。此外,您的产品 table 绝不会出现在连接子句中。它也应该在from.
$query = $this->db->select("p.id, l1.name as plname, l2.name as rcname")
->join("location l1", "l1.id = p.pickLocation", "left")
->join("location l2", "l2.id = p.recLocation", "left")
->get("product p");
我有一个 table 个产品和一个 table 个位置。 products table 有两列 pickLocation 和 recLocation。在位置 table 我有 id 和 name 列。 pickLocation 和 recLocation 的 id 来自位置 table。我怎样才能在 codeigniter 中加入 table。
这是我的代码
$this->db->select("locations.name as plname");
$this->db->select("locations.name as rcname");
$this->db->join("locations","locations.id=products.pickLocation","LEFT");
$this->db->join("locations","locations.id=products.recLocation","LEFT");
这是产品 table
+----+--------------+-------------+
| Id | pickLocation | recLocation |
+----+--------------+-------------+
| 1 | 12 | 23 |
| 2 | 12 | 12 |
+----+--------------+-------------+
这是位置table
+----+-----------+--+
| Id | name | |
+----+-----------+--+
| 12 | Location1 | |
| 23 | Location2 | |
+----+-----------+--+
我想要这样的结果
+-----------------------+
| 1 Location1 Location2 |
+-----------------------+
| 2 Location1 Location1 |
+-----------------------+
您需要使用别名,以便能够区分这两个联接。尝试这样的事情:
$this->db->select("pickLoc.name as plname");
$this->db->select("recLoc.name as rcname");
$this->db->join("locations as pickLoc","pickLoc.id=products.pickLocation","LEFT");
$this->db->join("locations as recLoc","recLoc.id=products.recLocation","LEFT");
使用别名。此外,您的产品 table 绝不会出现在连接子句中。它也应该在from.
$query = $this->db->select("p.id, l1.name as plname, l2.name as rcname")
->join("location l1", "l1.id = p.pickLocation", "left")
->join("location l2", "l2.id = p.recLocation", "left")
->get("product p");