Demeteor
Demeteor

Reputation: 1223

Java and mySQL select from related database index

I need some help understanding this.since I've never done something similar to this.

I have made a small database where 3 tables are related together. In that database I am selecting from a foreign key called cid and its related to a table where it has as primary key cid.

I made this query:

SELECT * 
FROM sales,continent,product 
WHERE sales.cid = continent.cid OR sales.pid = product.pid 

so I am trying to pull everything from the related databases. I can see it working on my code normally I think for example if execute the query and then I do a ResultSet to pull the string for example product.SKU i will get the expected result.

but did I really use my relation or I just pulled it cause my query is allowing me to do so?

Upvotes: 0

Views: 40

Answers (1)

forpas
forpas

Reputation: 164089

You want to join 3 tables, but this is not the way to do it.
Check this:

SELECT * 
FROM sales
INNER JOIN continent ON sales.cid = continent.cid
INNER JOIN product ON sales.pid = product.pid

This way you join continent to sales and product to sales by their related columns.
Depending on your needs you may use INNER or LEFT joins, but this is the way to go.
You can find more here: https://dev.mysql.com/doc/refman/8.0/en/join.html

Upvotes: 1

Related Questions