簡體   English   中英

MySQL查詢結果按查詢順序

[英]MySQL query result in query order

我正在嘗試執行一個查詢,該查詢在select子句中包含if和case語句,並且將得到的結果分組,因為每一行都包含一個索引為0的元素,該元素是一個數組,並且包含不包含if /的所有字段。 case和帶有if / case的case都在關聯數組中,表名作為鍵...我正在使用CakePHP模型的查詢方法

這是我查詢的一部分

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,它將不起作用。 知道為什么嗎?

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]);
    }
}

也許你想使用虛擬場

假設你在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
        )
    )
);

然后您可以使用哈希來獲取索引數組

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM