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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)