PHP / mySQL - 在'LIKE'子句中使用'CONCAT'和'AS'的结果

时间:2010-04-20 05:40:39

标签: php mysql sql-like concat

您好我有以下代码;

if( ! empty( $post['search-bar'] ) ) {
    $search_data = preg_replace("#\s\s#is", '', preg_replace("#[^\w\d\s+]#is", '', $post['search-bar'] ) );
    $data_array = explode( " ", $search_data );
    $data_array = "'%" . implode( "%' OR '%", $data_array ) . "%'"; 
    $query =  "
SELECT CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) AS 'STRING'
FROM `" . ACCOUNT_TABLE . "` 
WHERE STRING LIKE ( " . $data_array . " ) 
  AND BUSINESS_POST_CODE LIKE '" . substr(P_BUSINESS_POST_CODE, 0, 4) . "%'";

    $q = mysql_query( $query, $CON ) or die( "_error_" . mysql_error() );   
    if( mysql_num_rows( $q ) != 0 ) {
        die();
    }
}

问题是我想在where子句中使用临时颜色'STRING'但返回'unknown coloumn STRING任何人都可以指出我正确的方向,对菲尔来说

我打印出了查询;

SELECT PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME 
FROM nnn_accounts 
WHERE 
  CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) 
  LIKE ( '%web%' OR '%design%' ) 
AND BUSINESS_POST_CODE LIKE 'NG19%'

4 个答案:

答案 0 :(得分:1)

用'而不是'

包裹STRING

应该是: ..AS `STRING`

然后,您引用STRING的任何地方都应该是`STRING`

编辑:这应该解决它:

SELECT CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) AS `STRING`
FROM `" . ACCOUNT_TABLE . "` 
WHERE BUSINESS_POST_CODE LIKE '" . substr(P_BUSINESS_POST_CODE, 0, 4) . "%'"
HAVING `STRING` LIKE ( " . $data_array . " ) ;

答案 1 :(得分:1)

是的,看起来你不能在where子句中使用temp col 只需按原样指定:

WHERE CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) LIKE '%$data_array%') 

答案 2 :(得分:0)

删除'STRING'

周围的引号

答案 3 :(得分:0)

更合适的语法是

SELECT CONCAT( PROFILE_PROFFESION, FIRST_NAME, LAST_NAME, DISPLAY_NAME) AS `STRING` FROM `tablename` WHERE BUSINESS_POST_CODE LIKE 'NG19%' HAVING `STRING` LIKE '%web%' OR `STRING` LIKE '%design%'