dqwnxdhb88531 2018-06-14 15:43
浏览 96
已采纳

在join语句中给出别名而不更改codeigniter中的select语句(*)

I'm working with queries in espressionengine, and I'm coding them with the codeigniter syntax. Here's my query:

$sql = ee()->db->select('*');
$sql = ee()->db->from('sometable');
$sql = ee()->db->where('id', $hidden_id);
$sql = ee()->db->get();

I'd like to add a join in order to get another value like that:

 $sql = ee()->db->join('another_table', 'sometable.another_table_id, another_table.id);

Now the problem is that in the join statement I add another column with the same name (id).

Instead of changing the select statement (*) I'd like to add an alias in the join statement something like:

 $sql = ee()->db->join('another_table', 'sometable.another_table_id, another_table.id as another_table_id);

Is it doable?

  • 写回答

1条回答 默认 最新

  • dqayok7935 2018-06-15 13:19
    关注

    Hope this will help you :

    And your query should be like this :

    Use this if both primary and foreign key has same column name :

    $this->db->select('*');
    $this->db->from('sometable stable');
    $this->db->join('(SELECT id  
    FROM another_table ) as a_table' ,'id');
    $query = $this->db->get();
    print_r($query->result());
    

    if not please use this :

    Make sure in join here a_table.id = stable.id , primary and foreign key match here

    $this->db->select('*,a_table.id as credit_id');
    $this->db->from('sometable stable');
    $this->db->join('another_table a_table','a_table.id = stable.id');
    $query = $this->db->get();
    
    
    print_r($query->result());
    

    You can also get result from this query with the help of $this->db->query();

    $sql = 'your query here';
    $query = $this->db->query();
    print_r($query->result());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图