dongxie2613 2015-10-10 23:22
浏览 30
已采纳

错误号:1066不唯一的表/别名:codeigniter中的'service'

Want to get all services in one to many relationship My code is

$this->db->select('*');
    $this->db->from('service');
    $this->db->join('user', 'user.user_email = service.user_email', 'inner');
    $this->db->join('service', 'service.user_email = user.user_email', 'inner');
    $query = $this->db->get();

But it gives me an error

Error Number: 1066

Not unique table/alias: 'service'

SELECT * FROM (`service`) INNER JOIN `user` ON `user`.`user_email` = `service`.`user_email` INNER JOIN `service` ON `service`.`user_email` = `user`.`user_email`

Filename: C:\xampp\htdocs\service\system\database\DB_driver.php

If I do without

$this->db->from('service');

Then it gives syntax error

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN `user` ON `user`.`user_email` = `service`.`user_email` INNER JOIN `se' at line 2

SELECT * INNER JOIN `user` ON `user`.`user_email` = `service`.`user_email` INNER JOIN `service` ON `service`.`user_email` = `user`.`user_email`

Filename: C:\xampp\htdocs\service\system\database\DB_driver.php
  • 写回答

1条回答 默认 最新

  • douchui3933 2015-10-10 23:39
    关注

    You are trying to join the service table to the user table and then trying to join the service table again.

    This isn't something you should generally be trying to do, and when you do it this way, the rendered SQL statement contains two attempts to reference the service table. It's the second JOIN that causes the database pain and it throws the error that you're seeing.

    In this instance, just use:

    $this->db->select('*');
    $this->db->from('service');
    $this->db->join('user', 'user.user_email = service.user_email', 'inner');
    $query = $this->db->get();
    

    Since the

    $this->db->join('service', 'service.user_email = user.user_email', 'inner');
    

    is redundant here (you've already joined these tables on that field.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料