user3144640
user3144640

Reputation: 113

Sql one to many relationship

In my application I have the following Clients, Products. I want for each Client to have certain Products( one to many relationship) my code :

Tables Creation :

Create Table Client 
(
IDC int identity(1,1) not null primary key,
NumeC nvarchar(50),
CIF nvarchar(50) unique
)
Create Table Produs
(
IDP int identity(1,1) not null,
NumeP nvarchar(50),
Cantitate int,
IDC int 
)

this the Foreign Key :

Alter table Produs add constraint FK_Client_Produs_IDC
Foreign key (IDC) references Client(IDC)

Select statement Query to join Clients and foreach client to show Products :

Select NumeC,CIF from Client
Inner Join Produs
ON Client.IDC = Produs.IDC

I don't know what am I doing wrong, I just want to show Products for each client. It does not give me that. It just repeats the name of the client, now showing me The products for each client

Upvotes: 0

Views: 132

Answers (1)

user275683
user275683

Reputation:

In your SELECT you don't ever include anything from the Produs table, so why would it show it to you.

Select NumeC,CIF,NumeP
from Client
Inner Join Produs
ON Client.IDC = Produs.IDC

Upvotes: 3

Related Questions