Skip to content
Advertisement

mysql: natural join

everyone! I’m currently trying to make a simple table with MySQL that satisfies the below condition.

make a table of ship names, displacement, and numGuns with ones participated ‘Guadalcanal’ battle

enter image description here enter image description here enter image description here

3 tables are needed to make this.

I’ve tried several codes but they all failed.

I also wonder if I can use ‘natural join’ redundantly.

Here’s the code I’ve been trying

I added data input query for the case you might need it.

Big Thanks beforehand!

Advertisement

Answer

You want to show data of two tables: ships and classes they are 1:n related, so just join them (inner join on / using). Then you have the condition to only consider ships that partcipated in a particular battle. Conditions belong in the WHERE clause ideally. We look up data in other tables with EXISTS or IN.

By putting the condition in the WHERE clause we avoid getting any duplictates. Well, in this case (only one battle) we wouldn’t get duplicates anyway, but consider looking at more than one battle. A join would give us the ships multifold, as a join sais “give me the ships combined with their battles”, while IN and WHERE just say “give me the ships when they participated in at least one of the battles”.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement