我一直在尝试使用别名解决已点燃的数据表库上的自左联接问题。有什么想法吗?
在控制器中:
$this->datatables->select("t1.id_categories AS t1id, t1.name_categories AS t1name, t2.id_categories AS t2id, t2.name_categories AS t2name, t3.id_categories AS t3id, t3.name_categories AS t3name, t1.description_categories, t1.icon_categories, t1.slug_categories");
$this->datatables->from('categories t1');
$this->datatables->join('categories t2','t2.parent_categories = t1id','left');
$this->datatables->join('categories t3','t3.sub_parent_categories = t1id','left');
return $this->datatables->generate();
在视图中:
{data: "t1id", width: '5%', className: 'text-center', orderable: false},
{data: "t1.name_categories"},
{data: "t1.description_categories"},
{data: "t1.icon_categories", className: "text-center", searchable: false, orderable: false, render: function(data){
return '<a href="<?= base_url("upload/categories/") ?>'+data+'"><img src="<?= base_url("upload/categories/") ?>'+data+'" height=75 width=75></a>'
}},
{data: "t1.slug_categories"},
{data: "t2name", className: "text-center", searchable: false, orderable: false, render: function(data){
if (data == 0){
return "-"
} else {
return "ei"
}
}},
{data: "t3name"},
{data: "t1.status_categories", width: '20%', searchable: false, className: 'text-center', render: function(data){
if(data == '1') return 'Active'
else return 'Not Active'
}},
{name: "t1id", data: "tid", width: '15%', searchable: false, orderable: false, className: 'text-center', render: function(data){
return '<a title="Edit Data" href="javascript:void(0);" data-id='+data+' class="edit btn btn-success btn-sm"><i class="fa fa-edit"></i></a> <a title="Delete Data" href="javascript:void(0);" data-id='+data+' class="delete btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>'
}}
我收到此错误
Error Number: 1054
Unknown column 't1id' in 'on clause'
SELECT `t1`.`id_categories` AS `t1id`, `t1`.`name_categories` AS `t1name`, `t2`.`id_categories` AS `t2id`, `t2`.`name_categories` AS `t2name`, `t3`.`id_categories` AS `t3id`, `t3`.`name_categories` AS `t3name`, `t1`.`description_categories`, `t1`.`icon_categories`, `t1`.`slug_categories`
FROM `categories` AS `t1`
LEFT JOIN `categories` `t2` ON `t2`.`parent_categories` = `t1id`
LEFT JOIN `categories` `t3` ON `t3`.`sub_parent_categories` = `t1id`
$this->datatables->select("t1.id_categories AS t1id, t1.name_categories AS t1name, t2.id_categories AS t2id, t2.name_categories AS t2name, t3.id_categories AS t3id, t3.name_categories AS t3name, t1.description_categories, t1.icon_categories, t1.slug_categories");
$this->datatables->from('categories t1');
$this->datatables->join('categories t2','t2.parent_categories = t1.id_categories','left');
$this->datatables->join('categories t3','t3.sub_parent_categories = t1.id_categories','left');
return $this->datatables->generate();
使用t1
。id_categories
代替t1id
,它将可以完美工作