[thelist] Multi-level data storage

Adam Patrick apatrick at oracular.com
Wed Feb 28 08:47:38 CST 2001


> Okay, so you can use a composite primary key to link two tables together
> in a many-to-many situation. But how do you construct a query when the
> data is stored in two tables.
> 
> I could see how this could be achived with two queries (get the linkID by
> a query on table 2, then feed the matching linkID's into a query on table
> 1).
> 
> Does MySQL syntax include an easier or more efficient way to do this?
> 

I can't speak specifically to MySQL syntax but in SQL, this task is
accomplished using a "join".  Conceptually, each row in table 1 is
"joined" with every row in table 2 and then the results are limited by
the "join conditions", resulting in a picture of what the data would
look like, had it been stored in one table.  Using your example:

SELECT Url, LinkName, Description 
  FROM Links, LinkCats
 WHERE CatID $CatID
   and Links.LinkID = LinkCats.LinkID    -- <-- This is the join
condition





More information about the thelist mailing list