1

我有这个查询:

$query = 
         "
         SELECT
           detail_firstN AS firstname,
           detail_lastN AS lastname
         FROM users
         WHERE
           users_id = :users_id
         ";

结果将是这样的:

Array
(
    [firstname] => John
    [lastname] => Doe
)

MySQL 中有什么方法可以让我将结果组合成只有这个输出吗?

Array
(
    [name] => John Doe
)
4

2 回答 2

7

采用CONCAT

SELECT CONCAT(detail_firstN, ' ', detail_lastN) AS Name
FROM   users
WHERE  users_id = :users_id

或者CONCAT_WS

SELECT CONCAT_WS(' ', detail_firstN, detail_lastN) AS Name
FROM   users
WHERE  users_id = :users_id

更新 1

于 2012-11-30T09:14:43.800 回答
1

试试这个代码

<?PHP

function combine_result($array,$out_key = "name") // Deafult output key is [name]
{
    if(is_array($array))
    {
        $vals = array_values($array);
        $out  = implode(" ",$vals);
        return array($out_key=>$out);
    }
}


$array = array(
                "firstname" => "John",
                "lastname" => "Doe" );


print_r(combine_result($array)); // Array ( [name] => John Doe )


?>
于 2012-11-30T09:31:02.247 回答