php Codeigniter:订单子句中的列“id”不明确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8509798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:52:56  来源:igfitidea点击:

Codeigniter: Column 'id' in order clause is ambiguous

phpmysqlcodeigniteractiverecord

提问by daryl

I'm using CodeIgniter's Active Record Classes and I'm retrieving an error using the following code:

我正在使用 CodeIgniter 的 Active Record 类,我正在使用以下代码检索错误:

$this->db->select("*");
$this->db->order_by("id");
$this->db->limit($limit, $offset);
$this->db->from("atoms");
$this->db->join("atommeta", "atommeta.atom_id = atoms.atom_id");

$query  = $this->db->get();

It produces this error:

它产生这个错误:

Error Number: 1052

Column 'id' in order clause is ambiguous

SELECT * FROM (`atoms`) JOIN `atommeta` ON `atommeta`.`atom_id` = `atoms`.`atom_id` ORDER BY `id` LIMIT 10

Filename: /Applications/MAMP/htdocs/atom/models/atom_model.php

Line Number: 197

Line 197: $query = $this->db->get();

第 197 行: $query = $this->db->get();

Any ideas as to why? It seems to be something to do with the order_by

关于为什么的任何想法?好像和这个有关系order_by

回答by birderic

The error means that you are trying to order by a column name that is used in more than one table. Update your order_bystatement with the name of the table that has the column you want to order by. For example:

该错误意味着您正在尝试按在多个表中使用的列名进行排序。order_by使用包含要排序的列的表的名称更新您的语句。例如:

$this->db->order_by('atoms.id');

回答by nobsid

It looks like there is an idcolumn in both your atommetaand atomstables. Because you are joining these tables you will need to specify what column you want to order by.

看起来idatommetaatoms表格中都有一列。因为您要加入这些表,所以您需要指定要排序的列。

You will want

你会想要

$this->db->order_by("atoms.id");

or

或者

$this->db->order_by("atommeta.id");

回答by Damien Pirsy

You should specify which table that 'id' belogns to.

您应该指定 'id' 所属的表。

$this->db->select("*");
$this->db->from("atoms");
$this->db->join("atommeta", "atommeta.atom_id = atoms.atom_id");

pick one:

选一个:

$this->db->order_by("atommeta.id");

or

或者

$this->db->order_by("atoms.id");