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.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵