【问题标题】:concatenation in sql not working as expectedsql中的连接没有按预期工作
【发布时间】:2014-06-20 20:13:29
【问题描述】:

我正在尝试执行以下代码...

select a.name, a.phone, b.mobile, b.relation,
case
  when a.phone<>'' and b.mobile<>'' then a.phone + ' ' + b.mobile
  when a.phone<>'' and b.mobile='' then a.phone
  when a.phone='' and b.mobile<>'' then b.mobile
else ''
end as phone
from abc a join bcdb where a.id=b.id and b.relation='a123'

但是在执行第一种情况时,这些值是相加的,而不是被连接的……你能指导我吗

【问题讨论】:

  • 当我使用两个管道符号 (||) 时出现错误..
  • 列有哪些数据类型,您的 RDBMS - SQL Server、MySQL 是什么?
  • 发布您在问题中收到的错误。
  • 你的sql server版本?
  • 如果您想将数字(int、bigint 等)值与字符串连接,您必须转换或转换它们:technet.microsoft.com/en-us/library/aa276862%28v=sql.80%29.aspx

标签: mysql sql concatenation


【解决方案1】:

由于您使用的是 MySQL,因此您必须使用 CONCAT 函数来连接字符串,而不是 + 运算符。

select a.name, a.phone, b.mobile, b.relation,
case
    when a.phone<>'' and b.mobile<>'' then CONCAT(a.phone, ' ',  b.mobile)
    when a.phone<>'' and b.mobile='' then a.phone
    when a.phone='' and b.mobile<>'' then b.mobile
    else ''
end as phone
from abc a join bcdb where a.id=b.id and b.relation='a123'

备注:您应该注意任何操作数都不应该为NULL。

【讨论】:

    【解决方案2】:

    电话号码必须存储为数字而不是字符串。

    使用您的数据库平台转换功能。

    例子:

    CONVERT(VARCHAR(20), a.phone) + " " + CONVERT(VARCHAR(20), b.mobile)
    

    既然我们现在知道这是MySql,你可以试试:

    CONCAT(a.phone, " ", b.mobile)
    

    【讨论】:

    • 它们被存储为 varchar
    • 它们以数字相加。您确定它们存储为 varchars 吗?
    【解决方案3】:

    更新: 您似乎将电话和移动列存储为数字。因此,您必须将它们转换为字符串以进行连接。试试这个:

    CONVERT(a.phone, CHAR) + ' ' + CONVERT(b.mobile, CHAR)
    

    所以你的查询实际上变成了这样:

    select a.name, a.phone, b.mobile, b.relation,
    case
      when a.phone<>'' and b.mobile<>'' then CONVERT(a.phone, CHAR) + ' ' + CONVERT(b.mobile, CHAR)
      when a.phone<>'' and b.mobile='' then a.phone
      when a.phone='' and b.mobile<>'' then b.mobile
    else ''
    end as phone
    from abc a join bcdb where a.id=b.id and b.relation='a123'
    

    希望对你有帮助!!!

    【讨论】:

    • 我收到错误 - #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 a.phone' 和 b.mobile= 时在 'a.phone) + ' ' + CONVERT(NVARCHAR(30), b.mobile) 附近使用的正确语法'' 在第 3 行
    • @SamuelMathews 我已经更新了我的答案。看看吧
    • 值仍然被添加..而不是被连接:(
    【解决方案4】:

    我相信您的数据库会自动将您的电话列转换为整数值,这就是它添加它们而不是连接它们的原因。

    尝试强制将电话列转换为 varchar 数据类型。由于我不知道您使用的是什么数据库,所以我将举一个适用于 SQL Server 的示例。

    [编辑] 由于您使用的是 MySQL,因此您必须使用 CAST() 函数,而不是 CONVERT() 函数。 MySQL 中的 CONVERT() 函数用于转换编码。 查看文档:http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html [/编辑]

    select a.name, a.phone, b.mobile, b.relation,
    case
      when a.phone<>'' and b.mobile<>'' then CAST(a.phone as VARCHAR(50)) + ' ' + CAST(b.mobile AS VARCHAR(50))
      when a.phone<>'' and b.mobile='' then a.phone
      when a.phone='' and b.mobile<>'' then b.mobile
    else ''
    end as phone
    from abc a join bcdb where a.id=b.id and b.relation='a123'
    

    【讨论】:

    • 我收到此错误 #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'VARCHAR, a.phone) + ' ' + CONVERT(VARCHAR, b.mobile) 附近使用正确的语法,当 a.phone'' 和 b.mo' 位于第 3 行
    • 现在我知道您使用的是 MySQL,我能够根据数据库编辑我的答案。请再次检查。
    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多