使用codeigniter的php复杂查询连接

a6b3iqyw  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(401)

我有一个查询,它在phpmyadmin中生成结果,但在codeigniter中不生成结果。

  1. $sql = "SELECT express_interests.*,
  2. cl_to .User_Name AS ToClient,
  3. cl_from.User_Name AS FromClient,
  4. cl_from.Member_Id AS FromMid,
  5. cl_to.Member_Id AS ToMid
  6. FROM express_interests
  7. INNER JOIN users AS cl_to ON cl_to.User_Id = express_interests.To_Id
  8. INNER JOIN users AS cl_from ON cl_from.User_Id = express_interests.User_Id";

我想在codeignator中使用相同的查询。这就是我用过的

  1. $this->db->select('express_interests.*,
  2. cl_to .User_Name AS ToClient,
  3. cl_from.User_Name AS FromClient,
  4. cl_from.Member_Id AS FromMid,
  5. cl_to.Member_Id AS ToMid
  6. ');
  7. $this->db->from('express_interests');
  8. $this->db->join('users AS cl_to', 'cl_to.User_Id = express_interests.To_Id');
  9. $this->db->join('users AS cl_from', 'cl_from.User_Id = express_interests.User_Id');

当我用这个的时候它说
“字段列表”中的未知列“cl\u to.user\u name”
在codeigniter中使用上述查询的正确方法是什么。

hjzp0vay

hjzp0vay1#

  1. $this->db->select('express_interests.*,
  2. cl_to .User_Name AS ToClient,
  3. cl_from.User_Name AS FromClient,
  4. cl_from.Member_Id AS FromMid,
  5. cl_to.Member_Id AS ToMid
  6. ');

将此替换为

  1. $this->db->select('express_interests.*,
  2. cl_to.User_Name AS ToClient,
  3. cl_from.User_Name AS FromClient,
  4. cl_from.Member_Id AS FromMid,
  5. cl_to.Member_Id AS ToMid
  6. ');

表别名和列名之间不需要的间距

t98cgbkg

t98cgbkg2#

代替

  1. cl_to .User_Name AS ToClient,

具有

  1. cl_to .User_Name AS ToClient,

或者可以直接在codeigniter中运行sql语句

  1. $sql = "SELECT express_interests.*,
  2. cl_to .User_Name AS ToClient,
  3. cl_from.User_Name AS FromClient,
  4. cl_from.Member_Id AS FromMid,
  5. cl_to.Member_Id AS ToMid
  6. FROM express_interests
  7. INNER JOIN users AS cl_to ON cl_to.User_Id = express_interests.To_Id
  8. INNER JOIN users AS cl_from ON cl_from.User_Id = express_interests.User_Id";
  9. $result=$this->db->query($sql);
  10. print_r($result->result());
  11. die;
展开查看全部

相关问题