Relational Database Question?

I have three tables. Users - Documents - Connections

Users is just users.
Documents have id’s and names, among other things.
Connections is a giant list of userID’s and their corresponding documentID (each user has multiple rows connecting them with different documents)

I want to query a big list of document NAMES associated with a given userID.

Is there any way to do this without creating 100 queries?

Right now I have:

select docID from connections where userID = x

then I have PHP generate a bunch of queries in a for-loop using the docIDs from the first result in the form of:

select docName from documents where docID= varFromFirstResult

There must be a way to consolidate this into a single query. Can anyone help!?

I am pretty sure something like this would work:


select `connections`.`docID`, `documents`.`names` from connections, documents where `connections`.`userID` = x

SELECT connections.docID, documents.docName
FROM connections
INNER JOIN documents
ON connections.docID=documents.docID
WHERE connections.userID = x

Awesome thanks!

I never really understood Joins before but some w3c reading and I’m up and running!

For anyone else that stumbles here… Read this:
http://www.w3schools.com/Sql/sql_join.asp

You’re very welcome.

And yes, read up on joins, they are pretty much what makes complex SQL tick.