Nicolaj
Nicolaj

Reputation: 5

Inner join SQL 2 tables

I need help making a inner join.

I have to tables, one named Cycletype and the other Cykler.

Cycletype has a column named Type. In this column I have named the different type of cycles.

On my other table Cykler I have some columns with data about the cycles and a fk_cycletype_Id column so that they can link to eachother.

My problem is that I dont know how to write the code in my CS file.

This is what I have written so far(not working):

SELECT * FROM Cykler 
    INNER JOIN Cycletype 
    ON Cykler.fk_cycletype_Id = id 
    WHERE id = @id

I havent much experience with inner joins so I am completely lost and I am in big need of help.

On my frontend page I have 6 different pictures with the different types of cycles so that when I click on mountainbikes, for example, it should show all mountainbikes from Cykler. But i need help if anyone can, please?

https://i.sstatic.net/entX4.jpg

picture description of the tables

Upvotes: 0

Views: 70

Answers (3)

halterdev
halterdev

Reputation: 343

select * 
from Cykler c
inner join CycleType ct on ct.ID = c.fk_cycletype_Id 
where c.ID = @ID

You could then access the Type's by selecting ct.Type, or whatever the name of the Type field is in CycleTypes.

EDIT -- after seeing your second picture

This should work:

select ct.Type
from Cykler c
inner join Cycletype ct on ct.Cycletype_Id = c.fk_cycletype_Id
where c.ID = @ID

I put ct.Type in the select, but of course you could select whatever you need.

Upvotes: 0

jclozano
jclozano

Reputation: 628

Based on your question and comments, your query should be :

SELECT * FROM Cykler 
INNER JOIN Cycletype 
ON Cykler.fk_cycletype_Id = Cycletype.Cycletype_Id
WHERE Cycletype_Id = @id

Upvotes: 0

Mike S
Mike S

Reputation: 166

It's hard to tell without seeing the rest of your columns names...

But...is it possible that your "id" column exists in both tables... thus naming the table fixes the issue...

SELECT * FROM Cykler 
INNER JOIN Cycletype ON Cykler.fk_cycletype_Id = Cycletype.id 
WHERE Cycletype.id = @id

Upvotes: 1

Related Questions