0

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
}
3
  • this question sounds like you are trying to learn SQL just by "google, copy paste code and go..."... Go through some basic tutorial at least, if not school course. SQL needs change of thinking. Commented Apr 25, 2012 at 21:51
  • I am not trying to learn SQL at all. I'm just trying to get one simple task done and be done with it. I'm a Mongo guy. Commented Apr 25, 2012 at 22:01
  • That comment is like saying you want to multiply two numbers, but you can't be bothered to figure out how to use a calculator. If SQL were like rocket science I might see the point of not wanting to learn it, and if you were a beginner, I might see the point of this question. But the point of your question isn't that you don't know how to do it, it's that you don't want to spend time finding out (aka "TeH coDEZ"). The task is, as you rightly remark, simple, so why not just learn how to do it? Commented Oct 16, 2012 at 8:05

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.