【问题标题】:Concat() in mysql querymysql查询中的concat()
【发布时间】:2011-05-29 01:56:56
【问题描述】:

我在 mysql 查询中连接字符串时遇到问题。

这是我的查询:

SELECT *, CONCAT(nombre,' ',apellido) AS fullname FROM user WHERE nombre LIKE '%$busqueda%' OR apellido LIKE '%$busqueda%' OR email LIKE '%$busqueda%' OR about LIKE '%$busqueda%' OR place LIKE '%$busqueda%' or fullname LIKE '%$busqueda%'

但是查询失败: Query failed: Unknown column 'fullname' in 'where clause'

可能只是一个语法错误,谢谢

【问题讨论】:

标签: php mysql concatenation concat


【解决方案1】:

您不能在 WHERE 子句中引用别名。

可以使用 AS alias_name 为 select_expr 指定别名。别名用作表达式的列名,可用于 GROUP BY、ORDER BY 或 HAVING 子句。

不允许在 WHERE 子句中引用列别名,因为在执行 WHERE 子句时可能尚未确定列值。见Section C.5.5.4, “Problems with Column Aliases”.

Source

你可以改变:

... OR fullname LIKE '%$busqueda%'

... or CONCAT(nombre,' ',apellido) LIKE '%$busqueda%'

顺便说一句,所有这些 LIKE 都会使您的查询变得非常缓慢。您可能想查看full text search

【讨论】:

    【解决方案2】:

    您无法对在 SELECT 语句中创建的字段进行比较。

    使用

    or CONCAT(nombre,' ',apellido) LIKE %$busqueda%'
    

    请注意,这可能无法针对引擎进行优化,因此搜索可能会非常缓慢。如果这是针对大量数据,我会考虑保留一个单独的连接列来进行搜索。

    【讨论】:

      【解决方案3】:

      您还需要将 Concat() 放在 WHERE 中:

      ...place LIKE '%$busqueda%' OR CONCAT(nombre,' ',apellido) LIKE '%$busqueda%'
      

      【讨论】:

        【解决方案4】:

        使用:

        SELECT *, CONCAT(nombre,' ',apellido) AS fullname FROM user WHERE nombre LIKE '%$busqueda%' OR apellido LIKE '%$busqueda%' OR email LIKE '%$busqueda%' OR about LIKE '%$busqueda%' OR place LIKE '%$busqueda%' or CONCAT(nombre,' ',apellido) LIKE '%$busqueda%'
        

        【讨论】:

          【解决方案5】:

          正如已经指出的那样,您不能在WHERE 子句中使用列别名。这是因为在找到任何值之前就使用了WHERE 子句,因此在检索到行之前不会计算列别名中的表达式。

          现在,如果您不想执行两次 CONCAT(SELECT 中的一次,WHERE 中的一次),您可以利用 HAVING 子句。 HAVING 子句的工作方式与 WHERE 类似,只是在检索到行之后使用它。

          阅读更多:http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html

          SELECT *, CONCAT(nombre,' ',apellido) AS fullname FROM user WHERE nombre LIKE '%$busqueda%' OR apellido LIKE '%$busqueda%' OR email LIKE '%$busqueda%' OR about LIKE '%$busqueda%' OR place LIKE '%$busqueda%' HAVING fullname LIKE '%$busqueda%'

          请注意,如果您正在寻找最佳性能,这可能不一定能奏效。

          【讨论】:

          • 是的,除了因为 nombre 或 apellido 本身都不匹配,可能匹配 fullname 的行将被 WHERE 子句排除,因此永远不会进入 having 子句。您需要将整个事情更改为拥有(没有 WHERE)。但这确实效率低下……
          【解决方案6】:

          sintax 害死我了....

          下一个查询也有问题:

          $cadbusca="SELECT * , MATCH (nombre, apellido, email, about, place, CONCAT(nombre,' ',apellido)) AGAINST ('$busqueda') AS Score FROM user WHERE MATCH (nombre, apellido, email, about, place, CONCAT(nombre,' ',apellido)) AGAINST ('$busqueda') ORDER BY Score DESC";
          

          错误:

          Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(nombre,' ',apellido)) AGAINST ('dan stern') AS Score FROM user WHERE MATCH (nom' at line 1
          

          【讨论】:

            【解决方案7】:

            连接您自己的字符串,一个参数是字符串,第二个参数是您要使用此 CONCAT 函数代码连接的列名

            SELECT concat('ConcatString', columnName) AS newColumnName FROM products WHERE 1 LIMIT 0 , 30

            【讨论】:

              猜你喜欢
              • 2015-12-24
              • 2013-05-16
              • 2011-05-29
              • 1970-01-01
              • 2012-11-25
              • 2015-04-01
              • 2012-01-18
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多