Search code examples
mysqlusing

using a mysql table to access a lookup table?


I have two tables. Lets say they look like this

Table Sports:
Column 1: id (integer)
Column 2: name (varchar 100)

Table Sport Articles:
Column 1: id (integer)
Column 2: data (text)
Column 3: sport (integer)

So what I want to do is select things from the sports articles. lets say I have the ID number already. all i want is the data and the name of the sport.

So i want to select data from Sport articles where id = some number I already have, and name from Sports where id = sport from sport articles.

I think it uses the using keyword so my guess would be:

SELECT sportsarticles.data, sportsarticles.sport as sportid WHERE sportsarticles.id=5 AND sports.id=sportid

Solution

  • Yes. It is ok.

    SELECT DISTINCT sports.id, sportsarticles.data 
    FROM sportsarticles, sports
    WHERE sports.id = (YOURID) 
       AND sports.id = sportarticles.sports