我有这个问题:
$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
)
答案 0 :(得分: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
答案 1 :(得分: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 )
?>