MySQL查询结果以查询顺序排列

时间:2014-03-10 17:22:14

标签: php mysql cakephp

我正在尝试执行一个查询,其中包含select子句中的if和case语句,并且我得到的结果被分组,因为每行包含一个索引为0的元素,它是一个数组,它包含所有的字段没有if / case,if / case的那些是关联数组,表名为关键...我正在使用CakePHP model's query method

以下是我的查询的一部分

select 
    DATE_FORMAT(TIMESTAMPADD(HOUR, 4, request.creation_time), '%e-%m-%Y %H:%i:%S') As 'Request Date Received',
    request.request_uuid As 'Customer Id',
    contact_information.person_name As 'Customer Name',
    service_type.id As 'service_types',
    service.id AS 'lead_types',
    IFNULL(user.username, 'N/A') As 'Internal Owner of lead',
    request.request_status As 'status_types',
    IFNULL(DATE_FORMAT(TIMESTAMPADD(HOUR, 4, request_activity.creation_time), '%e-%m-%Y'), 'N/A') AS 'Date of last status',
    IFNULL(request_activity.comment, 'N/A') As comment_on_status_internal

结果看起来像这样

2014-03-10 16:57:32 Debug: Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Request Date Received] => 10-02-2014 01:49:02
                    [Internal Owner of lead] => rea
                    [Date of last status] => 10-02-2014
                    [Time of last status] => 18:39:56
                )

            [request] => Array
                (
                    [Customer Id] => 20140210000001DR
                    [status_types] => 10
                )

            [contact_information] => Array
                (
                    [Customer Name] => Jihane
                )

            [service_type] => Array
                (
                    [service_types] => Move
                )

        )

    [1] => Array ....

我希望结果看起来像这样

    2014-03-10 16:57:32 Debug: Array
(
    [0] => Array
        (
                    [Request Date Received] => 10-02-2014 01:49:02
                    [Customer Id] => 20140210000001DR
                    [Customer Name] => Jihane
                    [service_types] => Move
                    [Internal Owner of lead] => rea
                    [Date of last status] => 10-02-2014
                    [Time of last status] => 18:39:56
                    [status_types] => 10

        )

    [1] => Array ...

我已经尝试了没有任何case语句的查询,它看起来不错但是如果我添加IF / CASE它不起作用。知道为什么吗?

2 个答案:

答案 0 :(得分:0)

CakePHP的模型查询方法默认返回一个关联数组。您的索引返回值很有趣。可能有一个CakePHP方法返回不同的数组。如果你愿意,你可以研究一下。与此同时,这是一种平整阵列的方法。这假设您的数组存储在变量$ results:

foreach ($results as $rkey => $rvalue) {//0 => array, 1 => array
    foreach ($rvalue as $key => $value) {//0, request, contact_information...
        foreach ($value as $k => $v) {

            //move element to first sub-array slot
            $results[$rkey][$k] = $v;

            //unset associative element in sub-array
            unset($results[$rkey][$key][$k]);
        }
        //unset associative element in array
        unset($results[$rkey][$key]);
    }
}

答案 1 :(得分:0)

也许你想使用虚拟场

假设您在RequestsController

$this->Request->virtualFields['Internal_Owner_of_lead'] = "IFNULL(user.username, 'N/A')";
$this->Request->virtualFields['Date_of_last_status'] = "IFNULL(DATE_FORMAT(TIMESTAMPADD(HOUR, 4, request_activity.creation_time), '%e-%m-%Y'), 'N/A')";
// etc ....

$data = $this->Request->find(
    'all',
    array(
        'fields' => array(
            'Request.Internal_Owner_of_lead', 
            'Request.Date_of_last_status',
            // other fields here
        )
    )
);

然后你可以使用hash来获取一个索引数组

$data = Hash::extract($data, '{n}.Request');