【问题标题】:I am new in codeigniter and i am trying to join 3 table but it gives some issu我是 codeigniter 的新手,我正在尝试加入 3 个表,但它给出了一些问题
【发布时间】:2019-03-04 07:01:19
【问题描述】:

我有 3 张桌子,例如 ts_usersts_acc_category ts_user 我正在尝试像这样加入

$this->db->select('*');

$this->db->from('ts_voucher');
$this->db->join('ts_users','ts_users.user_id = ts_voucher.id');
$this->db->join('ts_acc_category','ts_voucher.user_id = ts_acc_category.acc_cat_id');

$this->db->where('user_reg_type','bill_party');

错误是

<h1>A Database Error Occurred</h1>
        <p>Error Number: 1054</p><p>Unknown column 'ts_voucher.user_id' in 'on clause'</p><p>SELECT *
FROM `ts_voucher`
JOIN `ts_users` ON `ts_users`.`user_id` = `ts_voucher`.`id`
JOIN `ts_acc_category` ON `ts_voucher`.`user_id` = `ts_acc_category`.`acc_cat_id`
WHERE `user_reg_type` = 'bill_party'</p><p>Filename: models/reports/ExpensesModel.php</p><p>Line Number: 32</p> </div>

请告诉我我的代码哪里错了

【问题讨论】:

  • 字段user_reg_type属于哪个表
  • 在 ts_users 表中
  • 在用户和凭证的join中,你尝试使用`ts_voucher`.`id`;在下一次加入中,我看到了`ts_voucher`.`user_id`
  • ts_voucher 的表中有两个字段,所以我还添加了两个用于连接的不同 id):
  • 我还告诉@KhanMuntazar 分享您的表结构,以便我们可以解决他们的问题

标签: php mysql sql codeigniter


【解决方案1】:
$this->db->select('tv.*,tu.*,tac.*'); //select field what you might want to select. 
        $this->db->from('ts_voucher as tv');
        $this->db->join('ts_users tu','tu.user_id = tv.id', 'left');
        $this->db->join('ts_acc_category as tac','tv.user_id = tac.acc_cat_id', 'left');
        $this->db->where('tv.user_reg_type','bill_party');

【讨论】:

  • 知道它给了我这个错误

    错误号:1054

    'on Clause'中的未知列'ts_voucher.user_id'

  • 给我 id ts_voucher 表的名称检查正确的数据库字段名称
  • ts_voucher id 名称为“id”
  • 检查已编辑的答案和数据库记录中的 ts_users 是否存在
  • 是的兄弟 (: (:
【解决方案2】:

=>试试这个..

$this->db->select('tv.*,tu.*,tac.*'); //select field what you might want to select. 
        $this->db->from('ts_voucher as tv');
        $this->db->join('ts_users tu','tu.user_id = tv.id', 'left');
        $this->db->join('ts_acc_category as tac','tv.user_id = tac.acc_cat_id', 'left');
        $this->db->where('tv.user_reg_type','bill_party');

【讨论】:

    【解决方案3】:

    您的 ts_voucher 表中没有 user_id 字段,请先检查

    【讨论】:

    • 对不起,我没听懂你在说什么
    • 错误提示您的 ts_voucher 表中没有 user_id 字段。如果可能的话,您能否分享问题中的表结构
    • 表结构上传请检查
    • 您必须在 ts_voucher 中创建一个新字段,即 user_id 并将 user_id 也保存在那里。之后只有您可以获取记录
    猜你喜欢
    • 1970-01-01
    • 2020-09-02
    • 2019-03-09
    • 2011-08-25
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    相关资源
    最近更新 更多