dongliqian6245 2018-02-09 07:56
浏览 38
已采纳

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

i am working in codeigniter project when i try to get datas from database using 5 tables in a single query.

In phpmyadmin sql query works fine but codeigniter query not working.

My Phpmyadmin Query :

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 '
') 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

My Codeigniter query:

->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 '
') 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 nothing but database prefix

  • 写回答

2条回答 默认 最新

  • duangonglian6028 2018-02-09 09:44
    关注

    You need to add sma in table name, it is part of table name.

    Query:

    $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 '
    ') 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();
    

    You can debug you query by search and set this as true,

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

    in your config/database.php

    Conclusion : Issue was there in alias of table names, after fixing alias name everything worked fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?