将两行合并为一个字符串

时间:2014-08-20 18:53:39

标签: php mysql sql

我有动态数据库,我想在此表中搜索<​​/ p>

FieldID | Content | TypeID
--------------------------
ABC-123 | jon     | 1
EFG-456 | doe     | 1
HIJ-789 | man     | 1

所以我的SELECT查询看起来像这样:

SELECT 
    GROUP_CONCAT(fieldsContent.Content SEPARATOR '|*|') AS Content, 
    GROUP_CONCAT(fieldsContent.FieldID SEPARATOR '|*|') AS FieldID, 
FROM (`customers`) 
LEFT OUTER JOIN `fieldsContent` ON `fieldsContent`.`TypeID` = `customers`.`ID` 
GROUP BY `fieldsContent`.`TypeID` 
ORDER BY `customers`.`Created` DESC

结果看起来像这样

Array
(
    [0] => stdClass Object
        (
            [Content] => jon|*|doe|*|man
            [FieldID] => ABC-123|*|EFG-456|*|HIJ-789
        )
)

当我添加HAVING并仅搜索jon时,它会返回结果

HAVING Content LIKE "%jon%" 

但是,当我尝试搜索jon doe时,它将返回空结果

HAVING Content LIKE "%jon doe%"

由于字符串中不存在jon doe,因此只有jon|*|doe

那么如何在不使用SEPARATOR的情况下将这两行合并为一个字符串,以便在jon doe中搜索?

BUT !!请记住,我需要获取SEPARATOR因为我需要结合要在php中使用的数据。

例如:

$field = explode('|*|',$data->FieldID);
$content = explode('|*|',$data->Content);

foreach($field as $k => $FieldID){
    switch($FieldID){
        case 'ABC-123':
            $res['first_name'] = $content[$k];
        break;
        case 'EFG-456':
            $res['last_name'] = $content[$k];
        break;
        case 'HIJ-789':
            $res['gender'] = $content[$k];
        break;      
    }
}

任何想法将不胜感激:)

2 个答案:

答案 0 :(得分:7)

尝试这个#1

HAVING Content LIKE "%jon%doe%"

尝试这个#2

HAVING Content LIKE "%jon" AND Content LIKE "%doe%"

尝试这个#3

HAVING REPLACE(Content,'|*|',' ') LIKE "%jon doe%"

给它一个尝试! : - )

答案 1 :(得分:1)

使用: -

内容类似于“%jon%”和内容类似于“%doe%”