Stevo
Stevo

Reputation: 99

How to get name from another table when using a key in SELECT?

I know this may be an easy question, but I've been stumped on this for the past hour and am not sure what terms to lookup that accurately describe what I am trying to do.

I have a MySQL database with two tables. Countries and Regions. The Regions table has two columns, id and name. An example of a row would be 1, north-america.

In the Countries table, there's a column named RegionID that would have a 1 if the country's region is north-america.

How can I grab the "north-america" in my query instead of printing out "1"?

Here's my SELECT that I am stumped on:

SELECT A.name, A.regionID FROM countries A, regions B ORDER BY A.name ASC

Upvotes: 3

Views: 7688

Answers (4)

Charles Bretana
Charles Bretana

Reputation: 146499

SELECT A.name, A.regionID, b.Name
FROM countries A
   Join regions B 
      on B.id = A.RegionId
ORDER BY A.name ASC

Upvotes: 6

AdrianBR
AdrianBR

Reputation: 2588

select  c.name as country, r.name as region
from countries c  
inner join regions r 
on r.id=c.regionid

Upvotes: 0

rs.
rs.

Reputation: 27427

Try this

SELECT A.name, A.regionID 
FROM countries A JOIN regions B 
ON A.regionID  = B.ID 
ORDER BY A.name

Upvotes: 0

Roberto Navarro
Roberto Navarro

Reputation: 958

Try this

    SELECT A.name, A.regionID 
FROM countries A, regions B 
WHERE a.RegionID =  b.id
AND b.id = 1
ORDER BY A.name ASC

Upvotes: 2

Related Questions