user1022241
user1022241

Reputation:

MySql equivalent of a for-each loop

I am a total MySql noob. I have two tables, Computer and Technician. Computer has a foreign key of techID which associates it with a particular Technician entry and indicates which Technician last serviced a computer. Basically I want list the last technician to service every computer.

I thought to do something like:

SELECT techID FROM Computer

My issue is that for each techID in my result set, I want to grab the technician's name out of the Technician table and return that instead. Basically I wondering how to query and achieve the same result as something like:

results = SELECT techID FROM Computer
for-each(r in results){
    SELECT name FROM Technician WHERE techID = r.techID
}

Upvotes: 0

Views: 2871

Answers (1)

Roland Bouman
Roland Bouman

Reputation: 31961

Use a JOIN:

SELECT     Computer.id, Technician.name
FROM       Computer
INNER JOIN Technician
ON         Computer.techID = Technician.techID 

This should give you a list of each computer with the matching technician.

Upvotes: 2

Related Questions