简体   繁体   English

PHP mysql选择concat

[英]PHP mysql select concat

i have this function that shows an autosuggest in a form: 我有此功能,以一种形式显示自动建议:

function searchbyId($params) {

    $input = strtolower($params['input']);
    $len = strlen($input);
    $limit = isset($params['limit']) ? (int) $params['limit']:25;
    $items=array();
    $sql='SELECT DISTINCT nIdentidad, CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre FROM tarjeta_indent WHERE nIdentidad LIKE \''.$input.'%\' ORDER BY nIdentidad LIMIT '.$limit;
    $resp=db_query($sql);
    if($resp && db_num_rows($resp)){
        while(list($nIdentidad)=db_fetch_row($resp)) {
            //$name=(strpos($name,'@')===false)?$name:'';
            $items[] ='{"id": "'.$nIdentidad.'", "value": "'.$nIdentidad.'"}';

        }
    }
    $result= '{"results": ['.implode(", ", $items).']}';
    return $result;
}

but only works if i change the query to this: 但仅在我将查询更改为此时才有效:

$sql='SELECT DISTINCT nIdentidad FROM tarjeta_indent WHERE nIdentidad LIKE \''.$input.'%\' ORDER BY nIdentidad LIMIT '.$limit;

How can i do the concat part? 我该如何做concat部分?

Thanks. 谢谢。

I feel like this is decidedly simple, but you have a syntax error in your concat statement; 我感觉这很简单,但是您的concat语句中有语法错误。 you're using single quotes to escape strings but the PHP string is defined with single quotes: 您使用单引号将字符串转义,但是PHP字符串定义为单引号:

$sql='SELECT DISTINCT nIdentidad, CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre FROM tarjeta_indent WHERE nIdentidad LIKE \''.$input.'%\' ORDER BY nIdentidad LIMIT '.$limit;

How about this? 这个怎么样?

$sql = sprintf(<<<SQL
    SELECT
      DISTINCT nIdentidad,
      CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre
    FROM tarjeta_indent
    WHERE nIdentidad LIKE '%s'
    ORDER BY nIdentidaa
    LIMIT %d
SQL
  , mysql_real_escape_string($input), $limit);

If I'm right about db_query and you're using Drupal, try this instead (for Drupal 7): 如果我对db_query是正确的并且您正在使用Drupal,请尝试以下方法(对于Drupal 7):

$sql = <<<SQL
    SELECT
      DISTINCT nIdentidad,
      CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre
    FROM tarjeta_indent
    WHERE nIdentidad LIKE :input
    ORDER BY nIdentidaa
    LIMIT :limit
SQL;
$resp = db_query($sql, array(':input' => $input, ':limit' => $limit));

This would be the Drupal 6 version: 这将是Drupal 6版本:

$sql = <<<SQL
    SELECT
      DISTINCT nIdentidad,
      CONCAT(primerNombre, ' ', segundoNombre, ' ', primerApellido, ' ', segundoApellido) AS nombre
    FROM tarjeta_indent
    WHERE nIdentidad LIKE %s
    ORDER BY nIdentidaa
    LIMIT %d
SQL;
$resp = db_query($sql, $input, $limit);

CONCAT will work with strings/varchar, but not with numbers. CONCAT将适用于字符串/ varchar,但不适用于数字。 So if primerNombre and segundNombre are INT data types, CONCAT will fail. 因此,如果primaryNombre和segundNombre是INT数据类型,则CONCAT将失败。 Try using the CONVERT function: 尝试使用CONVERT函数:

SELECT DISTINCT nIdentidad, 
       CONCAT( CONVERT(primerNombre, char(8)), ' ', 
               CONVERT(segundoNombre, char(8))  ) as nombre

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM