dougu2006 2012-02-06 10:00
浏览 62
已采纳

Codeigniter mysql连接两次相同的表

Solution

I figured that the codeigniter call missed out the possibility to do a AS inside of a join function, or atleast thats what i know so far, any correction will be appreciated. Therefor i did this instead of the original call

        $sql = "
            SELECT default_mailsystem.*,
                recipent.first_name AS modtager, 
                sender.first_name AS afsender

            FROM default_mailsystem

            LEFT JOIN default_profiles AS recipent 
                ON recipent.id = default_mailsystem.id

            LEFT JOIN default_profiles AS sender 
                ON sender.id = default_mailsystem.id
    ";
    return $this->db->query($sql)->result();

Question

Im trying to make a mailsystem in Codeigniter with the PyroCms. In my mail table i have a "recipent" row and a "sender" row which contains the user id of the sender and recipent. To retrive usernames from the ids im trying to join the table toghetter, but it simply returns me this error:

Error Number: 1066

Not unique table/alias: 'default_users'

SELECT `default_mailsystem`.*, `default_users`.`username` AS modtager, `default_users`.`username` as afsender FROM (`default_mailsystem`) LEFT JOIN `default_users` ON `default_mailsystem`.`recipent` = `default_modtager`.`id` LEFT JOIN `default_users` ON `default_mailsystem`.`sender` = `default_afsender`.`id` ORDER BY `id` DESC

Filename: /hsphere/local/home/brightmedia/reuseable.dk/modules/mail/models/mail_m.php

Line Number: 13

My code is as follows:

$this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
            ->join('users', 'mailsystem.recipent = modtager.id', 'left')
            ->join('users', 'mailsystem.sender = afsender.id', 'left');
        $this->db->order_by('id', 'DESC');
        return $this->db->get('mailsystem')->result();

The funny thing is, that if i remove the last "join" operation and leave it to only join the recipent of the mail it all works out well.

Any suggestions will be appreciated. Sincerly Jonas

  • 写回答

5条回答 默认 最新

  • dongshangan2074 2012-04-10 13:19
    关注

    This is very simple

    $this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
    $this->db->join('users', 'mailsystem.recipent = modtager.id AND mailsystem.sender = afsender.id', 'left')
    $this->db->order_by('id', 'DESC');
    return $this->db->get('mailsystem')->result();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么