简体   繁体   中英

SQL retrieve first & last name based on ID from other table

Currently I'm stuck with something that I think is easy to do but I'm going to ask it anyway.

I have a table called zg_data which looks like this:

+---------------------+------------------+
|       dataID        |      employee    |
+---------------------+------------------+
|         12          |        123       |
+---------------------+------------------+

and a table called zg_employees which looks like this:

+---------------------+------------------+------------------+
|      employeeID     |    Firstname     |     Lastname     |
+---------------------+------------------+------------------+
|          123        |       Test       |       User       |
+---------------------+------------------+------------------+

Now what I need to do is when selecting data from the zg_data table instead of it returning the employeeID I would like that it returns the Firstname & Lastname instead. I've been reading about joining tables but I haven't been able to figure it out just yet.

Kind regards in advance.

Well, it's basic JOIN operation, you should be able to figure it out on your own if you are familiar with it:

select d.dataID, e.Firstname, e.Lastname
from zg_data d join zg_employee e on d.employee = e.employeeID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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