Bir öğrencimin isteği üzerine yayınlıyorum! İyi Çalışmalar..
/*Amaç birden fazla kategoriye dahil edilebilen bir ürün kategorilendirme sisteminde
hangi üsünün hangi kategoriye ait olduğunu ve hangi kategoriye ait olmadığını bulmak!*/
--> Tablolarımız :
CREATE TABLE [dbo].[TBLCategories](
[CategoryID] [int] IDENTITY(1,1) NOT NULL,
[CatName] [varchar](50) NULL,
[RegisterDate] [datetime] NULL,
CONSTRAINT [PK_TBLCategories] PRIMARY KEY CLUSTERED
(
[CategoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[TBLCategories] ADD CONSTRAINT [DF_TBLCategories_RegisterDate] DEFAULT (getdate()) FOR [RegisterDate]
GO
CREATE TABLE [dbo].[TBLProducts](
[ProductID] [int] IDENTITY(1,1) NOT NULL,
[ProductName] [varchar](50) NULL,
[Price] [money] NULL,
[RegisterDate] [datetime] NULL,
CONSTRAINT [PK_TBLProducts] PRIMARY KEY CLUSTERED
(
[ProductID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[TBLProducts] ADD CONSTRAINT [DF_TBLProducts_RegisterDate] DEFAULT (getdate()) FOR [RegisterDate]
GO
CREATE TABLE [dbo].[TBLJoinProductCategory](
[JoinID] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NULL,
[CategoryID] [int] NULL,
[RegisterDate] [datetime] NULL,
CONSTRAINT [PK_TBLJoinProductCetegory] PRIMARY KEY CLUSTERED
(
[JoinID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TBLJoinProductCategory] ADD CONSTRAINT [DF_TBLJoinProductCetegory_RegisterDate] DEFAULT (getdate()) FOR [RegisterDate]
GO
--> Şimdi İstediğimiz sorguları yazalım!!
-- Öncelikli olarak LEFT OUTER JOIN anahtar kelimesi kullanmadan sonuç alalım!
-- Mouse Ürünü İçin!!
-- Bulunduğu Kategorileri Bulmakselect P.ProductName as 'Ürün Adı', CAT.CategoryID as 'Kategori ID', CAT.CatName as 'Bulunduğu Kategori'
from TBLJoinProductCategory JT , TBLCategories CAT, TBLProducts P
where P.ProductName='Mouse' and JT.CategoryID=CAT.CategoryID and JT.ProductID=P.ProductID
--Bulunmadığı Kategorileri Bulmakselect P.ProductName as 'Ürün Adı', CAT.CategoryID as 'Kategori ID', CAT.CatName as 'Bulunmadığı Kategori'
from TBLCategories CAT,TBLProducts P
where P.ProductName='Mouse' and CAT.CategoryID not in
(
select JT.CategoryID from TBLCategories CAT,TBLProducts P, TBLJoinProductCategory JT
where JT.CategoryID=CAT.CategoryID and P.ProductID=JT.ProductID and P.ProductName='Mouse'
)
--> Şimdide LEFT OUTER JOIN anahtar Kelimesiyle yapalım!
--Bu işlemleri Left Outer Join yada Right Outer Join İlede Yapabilirsiniz! select * from TBLCategories CAT left outer join TBLJoinProductCategory JT
on
JT.CategoryID=CAT.CategoryID and JT.ProductID=1
--> Product ID si 1 olan ürün için null olan yerler Bizim istediğimiz sonuçlar!
select CAT.CategoryID as 'Kategori ID',CAT.CatName as 'Bulunmadığı Kategori' from TBLCategories CAT left outer join TBLJoinProductCategory JT
on
JT.CategoryID=CAT.CategoryID and JT.ProductID=1 where JT.CategoryID is null
--> Yukarıdaki sorguya göre bulunmadığı kategorilerin ID numaraları ve İsimleri!
/* Yukarıda "Left outer join" anahtar kelimesi ile birleştirilmiş va herhangi bir join anahtar
kelimesi kullanmadan birleştirilmiş tablolardan dönen sonuçlar var.
İki sonuçta istediğimiz sonuç fakat "Left outer join" ile yaptığın birleştirme her zaman daha
efektif bir sonuç verecektir!. Hızlı olacaktır!
İyi Çalışmalar
Ali Emrah PEKESEN */
Hiç yorum yok:
Yorum Gönder