gpt4 book ai didi

php - MySql 查询在 PHPmyadmin 中工作,但在 codeigniter 中不工作

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:23 24 4
gpt4 key购买 nike

当我尝试在单个查询中使用 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 只不过是数据库前缀

最佳答案

需要在表名中添加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,

$db['default']['db_debug'] = true;

在你的 config/database.php 中

结论:问题出在表名的别名中,修复别名后一切正常。

关于php - MySql 查询在 PHPmyadmin 中工作,但在 codeigniter 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48701131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com