【问题标题】:How do I join two tables in SQL to grab the names from one table and show them in a query?如何在 SQL 中连接两个表以从一个表中获取名称并在查询中显示它们?
【发布时间】:2021-11-23 17:52:18
【问题描述】:

我有两张这样的表:

员工表

EmployeeID firstName lastName
1 Johnny Depp
2 Rebecca Smith
3 Rodger Doe

销售表

EmployeeID Sales
1 100.20
2 200.19
3 355.23

我想加入表格来做这样的事情:

EmployeeID fullName Sales
1 Johnny Depp 100.20
2 Rebecca Smith 200.19
3 Rodger Doe 355.23

我该怎么做?到目前为止,这是我尝试过的:

SELECT employee.firstName + employee.lastName AS fullName, employeeID, sales
FROM employee i 
INNER JOIN Sales s ON s.customerID = i.CustomerID

我的“+”符号出现语法错误。

我的问题是什么?

【问题讨论】:

  • 使用concat() 函数,MySQL 连接字符串的方式。

标签: mysql sql


【解决方案1】:

正如@Gordon 所说,使用CONCAT()

SELECT CONCAT(employee.firstName, ' ', employee.lastName) AS fullName
       , employeeID
       , sales
FROM employee i 
INNER JOIN Sales s 
   ON s.customerID = i.CustomerID 

【讨论】:

    猜你喜欢
    • 2013-12-14
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多