简体   繁体   English

SQL一对多关系

[英]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 它只是重复客户的名称,现在向我显示每个客户的产品

In your SELECT you don't ever include anything from the Produs table, so why would it show it to you. SELECT您永远不会包含Produs表中的任何内容,因此为什么要向您显示它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM