machinery
machinery

Reputation: 6290

Getting values in SQL from foreign key

I'm currently strungling with SQL. I have two tables with the following columns:

table "A": columns "_id", "B_id" and "type"
table "B": columns "_id", "x", "y", "z" 

"_id" is the row ID. "B_id" is a foreign key to a row ("_id") in table B.

Now I would like to get all "type" values of each row in table A together with the associated x, y and z value in table B.

I can get all rows in table A by:

SELECT type FROM A

Currently I have problems also retrieving the according x, y and z from B. How can this be done?

Upvotes: 0

Views: 71

Answers (2)

Peter
Peter

Reputation: 2181

SELECT A.type, B.x, B.y, B.z FROM A, B where A.B_id=B._id

or with more modern JOIN-Syntax:

SELECT A.type, B.x, B.y, B.z FROM A JOIN B ON A.B_id=B._id

This is for MySQL, but should work in other RDBMs.

Upvotes: 0

Ryan Gadsdon
Ryan Gadsdon

Reputation: 2378

This will give you the join as needed. Then just add the rows you need

 SELECT *

 FROM A
 JOIN b
 on A.B_ID = B._id

Upvotes: 2

Related Questions