1

我正在使用 CodeIgniter 并进行了这样的查询。

$this->db->select("first_name as 'First Name', last_name as 'Last Name', phone as 'Phone', os.group as 'Group', gender as 'Gender', birth_date as 'DOB', email as 'Email', street_address as 'Address', city as 'City', province as 'Province', postal_code as 'Postal Code', country 'Country', payment_amount as 'Payment Amount', DATE_FORMAT(payment_date, '%d/%m/%Y') as 'Payment Date', an.notes as 'Notes'");
                $this->db->join('athlete_notes an', 'os.id = an.id', 'inner');
                $this->db->group_by(array("first_name", "last_name"));
                $this->db->order_by("last_name asc, first_name asc");
                $query = $this->db->get('offline_shoppers os');

并得到这个 csv 输出:

header("Content-type: text/csv");
                header("Content-Disposition: attachment; filename=members_list.csv");
                header("Pragma: no-cache");
                header("Expires: 0");
                echo $this->dbutil->csv_from_result($query);

我正在尝试将 payment_date 格式化为 d/m/Y 但 DATE_FORMAT 给了我错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (`offline_shoppers` os) INNER JOIN `athlete_notes` an ON `os`.`id` = `an`.`' at line 2

我能做些什么来解决这个问题?

4

2 回答 2

1
$this->db->select("first_name as 'First Name', last_name as 'Last Name', phone as 'Phone', os.group as 'Group', gender as 'Gender', birth_date as 'DOB', email as 'Email', street_address as 'Address', city as 'City', province as 'Province', postal_code as 'Postal Code', country 'Country', payment_amount as 'Payment Amount', DATE_FORMAT(payment_date, '%d/%m/%Y') as 'Payment Date', an.notes as 'Notes'");
                $this->db->from('offline_shoppers as os');//
                $this->db->join('athlete_notes as an', 'os.id = an.id', 'inner');//
                $this->db->group_by(array("first_name", "last_name"));
                $this->db->order_by("last_name asc, first_name asc");
                $query = $this->db->get();
于 2013-09-06T21:12:14.667 回答
0

首先在 phpmyadmin 中运行这个原始 sql 查询

SELECT first_name AS 'First Name', last_name AS 'Last Name', phone AS 'Phone', os.group AS 'Group', gender AS 'Gender',
 birth_date AS 'DOB', email AS 'Email', street_address AS 'Address', city AS 'City', province AS 'Province', 
 postal_code AS 'Postal Code', country 'Country', payment_amount AS 'Payment Amount', 
 DATE_FORMAT(payment_date, '%d/%m/%Y') AS 'Payment Date', an.notes AS 'Notes'
FROM offline_shoppers os              
    INNER JOIN athlete_notes an ON (os.id = an.id)
    GROUP BY first_name,last_name
    ORDER BY  last_name ASC, first_name ASC 

如果没有发现错误,那么

$this->db->query("
SELECT first_name AS 'First Name', last_name AS 'Last Name', phone AS 'Phone', os.group AS 'Group', gender AS 'Gender',
 birth_date AS 'DOB', email AS 'Email', street_address AS 'Address', city AS 'City', province AS 'Province', 
 postal_code AS 'Postal Code', country 'Country', payment_amount AS 'Payment Amount', 
 DATE_FORMAT(payment_date, '%d/%m/%Y') AS 'Payment Date', an.notes AS 'Notes'
FROM offline_shoppers os              
    INNER JOIN athlete_notes an ON (os.id = an.id)
    GROUP BY first_name,last_name
    ORDER BY  last_name ASC, first_name ASC ")->result();
于 2013-09-06T21:56:51.243 回答