【问题标题】:MySQL String concatenation of Title and Name标题和名称的 MySQL 字符串连接
【发布时间】:2020-09-21 15:42:35
【问题描述】:

我有 2 个表格 Patient 表格和 Title 表格

患者表 3 字段

PatientTableID    FirstName PatTitleLookup 
1                  John      (Value Stored as integer)
2                  Mary      (Value Stored as integer)

标题表 2 字段

ID  Title
1      Mr.
2      Ms.

我想连接标题和名字

所以我用这个

SELECT CONCAT(`PatientTable`.`PatTitleLookup`, '. ', `PatientTable`.`FirstName`)
FROM `PatientTable`
WHERE `PatientTable`.`PatientTableID` = 1

我明白了 “1.John”而不是“Mr. John” “2.Mary”而不是“Ms. Mary”

请告知我确定我的语法错误

【问题讨论】:

  • 请告知如何使用join语句
  • 我添加了一个应该接近您需要的答案。如果它们不同,可能需要更改表/列名称。
  • @sam 编辑时请注意格式 - 您可以编辑已经格式化的格式。
  • 考虑处理应用代码中数据显示的问题

标签: mysql group-concat


【解决方案1】:

您需要加入titles 表才能从中获取值。使用on 了解数据应如何join

SELECT CONCAT(t.title, '. ', pt.FirstName)
FROM PatientTable as pt
join Title as t 
on pt.PatTitleLookup = t.id
WHERE pt.PatientTableID = 1

我还在此处为您的表使用了别名(as ptas t),这使得稍后在查询中的引用更短。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2014-03-23
    相关资源
    最近更新 更多