Reputation: 23
I created a Winforms app and a database with 2 tables in Visual Studio:
Member
- holds details like first nameMembership
- holds a membership type basic, VIP.I want to display membership type from the Membership
table and the FirstName
from the Member
table.
I have added the foreign key for the membership table to Member
. Now I try to create the view as below. While this works, when I show results of the view , it creates all the requested data 3 times:
MemberID FirstName Type
-------------------------------
1 Tommy Basic
2 Sammy VIP
3 Alley Basic
1 Tommy Basic
2 Sammy VIP
3 Alley Basic
1 Tommy Basic
2 Sammy VIP
3 Alley Basic
The code for the view:
CREATE VIEW [dbo].[Memberdetails]
AS
SELECT Member.MemberId, Member.FirstName, Membership.Type
FROM [Member], [Membership]
Not sure how to fix it just to display it once.
Upvotes: 0
Views: 58
Reputation: 56
You need add field MembershipId to Member table.
You can use where clause
CREATE VIEW [dbo].[Memberdetails]
AS
SELECT Member.MemberId, Member.FirstName, Membership.Type
FROM [Member], [Membership]
WHERE [MemberShip].[Id] = [Member].[MembershipId]
Or use join clause
CREATE VIEW [dbo].[Memberdetails]
AS
SELECT Member.MemberId, Member.FirstName, Membership.Type
FROM [Member]
JOIN [Membership] ON [Membership].[Id] = [Member].[MembershipId]
Upvotes: 1