简体   繁体   English

如何连接 SQL 中的两个表以从一个表中获取名称并在查询中显示它们?

[英]How do I join two tables in SQL to grab the names from one table and show them in a query?

I have two tables like this:我有两个这样的表:

Employee Table员工表

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

Sales Table销售表

EmployeeID    Sales 
1             100.20
2             200.19
3             355.23

And I'd like to join the tables to do something like this:我想加入表格来做这样的事情:

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

How would I do that?我该怎么做? Here's what I tried so far:这是我到目前为止所尝试的:

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

I'm getting a syntax error at my "+" symbol.我的“+”符号出现语法错误。

What's my problem?我的问题是什么? Thanks!谢谢!

as @Gordon said,use CONCAT() :正如@Gordon 所说,使用CONCAT()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM