我托管了一个代码启动器项目 here但我有错误号:1054网站最受欢迎部分显示的“on 子句”中的未知列“p.id”。
下面是给出此错误的 sql 查询。
public function get_popular()
{
// Using active records
$this->db->select('P.*, COUNT(O.product_id) as total');
$this->db->from('orders AS O');
$this->db->join('products AS P', 'O.product_id = p.id','INNER');
$this->db->group_by('O.product_id');
$this->db->order_by('total','desc');
$query = $this->db->get();
return $query->result();
}
用这行代码解决问题
$this->db->join('products AS P', 'O.product_id = p.id','INNER');
项目在我的本地机器上正常运行,直到我在这里托管它以供查看
这个很简单。您将表的别名声明为 P - 大写字母:
$this->db->join('products AS P', ...)
然后通过小写字母引用它:p.id
在 Windows 上,字母的大小写 - P
或 p
- 无关紧要,在 Linux 上(您的主机可能使用它)则有关系。因此,要修复此错误,只需键入:
$this->db->join('products AS p', 'O.product_id = p.id','INNER');
将来您最好对表和字段使用严格的小写名称,因为这适用于任何操作系统。
我是一名优秀的程序员,十分优秀!