当我尝试在单个查询中使用5个表从数据库获取数据时,我正在使用codeigniter项目。
在phpmyadmin中,sql查询工作正常但是codeigniter查询无效。
我的Phpmyadmin查询:
select sma_sales.date, sma_sales.reference_no, biller, sma_companies.name as DeliveryRep,customer,c.code,
GROUP_CONCAT(CONCAT(sma_sale_items.product_name, ' (', sma_sale_items.quantity, ')') SEPARATOR '\n') as iname,
grand_total, paid, sma_payments.amount as pyname,
payment_status
from sma_sales
left outer join sma_sale_items on sma_sale_items.sale_id=sma_sales.id
left outer join sma_companies on sma_companies.id=sma_sales.delivered_id
left outer join sma_companies as c on c.id=sma_sales.customer_id
left outer join sma_warehouses on sma_warehouses.id=sma_sales.warehouse_id
left outer join sma_payments on sma_payments.sale_id=sma_sales.id
group by sma_sales.id
我的Codeigniter查询:
->select("date, reference_no, biller, companies.name as DeliveryRep,customer,c.code, "
. "GROUP_CONCAT(CONCAT(" . $this->db->dbprefix('sale_items') . ".product_name,"
. " ' (', " . $this->db->dbprefix('sale_items') . ".quantity, ')') SEPARATOR '\n') as iname,"
. " grand_total, paid,payments.amount as pyname, payment_status", FALSE)
->from('sales')
->join('sale_items', 'sale_items.sale_id=sales.id', 'left')
->join('companies', 'companies.id=sales.delivered_id', 'left')
->join('companies as c', 'c.id=sales.customer_id', 'left')
->join('warehouses', 'warehouses.id=sales.warehouse_id', 'left')
->join('payments', 'payments.sale_id=sales.id', 'left')
->group_by('sales.id')
$q = $this->db->get();
sma只是数据库前缀
答案 0 :(得分:3)
您需要在表名中添加 sma ,它是表名的一部分。
<强>查询:强>
$this->db->select("date, reference_no, biller, sma_companies.name as DeliveryRep,customer,c.code, "
. "GROUP_CONCAT(CONCAT(" . $this->db->dbprefix('sale_items') . ".product_name,"
. " ' (', " . $this->db->dbprefix('sale_items') . ".quantity, ')') SEPARATOR '\n') as iname,"
. " grand_total, paid,sma_payments.amount as pyname, payment_status", FALSE)
->from('sma_sales')
->join('sma_sale_items', 'sma_sale_items.sale_id=sma_sales.id', 'left')
->join('sma_companies', 'sma_companies.id=sma_sales.delivered_id', 'left')
->join('sma_companies as c', 'c.id=sma_sales.customer_id', 'left')
->join('sma_warehouses', 'sma_warehouses.id=sma_sales.warehouse_id', 'left')
->join('sma_payments', 'sma_payments.sale_id=sales.id', 'left')
->group_by('sales.id')
$q = $this->db->get();
您可以通过搜索调试查询并将其设置为true,
你的config / database.php中的$ db ['default'] ['db_debug'] = true;
结论:问题是表名的别名,修复别名后一切正常。
答案 1 :(得分:0)
您正在使用5个表。可能在多个表中可以存在相同的列名。您可以为所有表使用别名并更改查询,如:
$this->db->select("s.date , s.reference_no, s.biller, companies.name as DeliveryRep,s.customer,c.code, "
. "GROUP_CONCAT(CONCAT(" . $this->db->dbprefix('sale_items') . ".product_name, "
. "' (', " . $this->db->dbprefix('sale_items') . ".quantity, ')') SEPARATOR '\n') as iname, "
. "s.grand_total, s.paid,pay.amount as pyname, s.payment_status", FALSE)
->from('sales as s')
->join('sale_items', 'sale_items.sale_id=s.id', 'left')
->join('companies', 'companies.id=s.delivered_id', 'left')
->join('companies as c', 'c.id=s.customer_id', 'left')
->join('warehouses', 'warehouses.id=s.warehouse_id', 'left')
->join('payments as pay', 'pay.sale_id=s.id', 'left')
->group_by('s.id')
->order_by('s.date desc');