my name
my name

Reputation: 3

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

Upvotes: 0

Views: 613

Answers (2)

nos
nos

Reputation: 229058

SELECT sports.name, 
       sportsarticles.data, 
       sportsarticles.sport AS sportid 
FROM   sports 
       INNER JOIN sportsarticle 
         ON sportsarticle.id = sports.id 
WHERE  sportsarticles.id = 5 

Upvotes: 1

Muneer
Muneer

Reputation: 7564

Yes. It is ok.

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

Upvotes: 0

Related Questions