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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序