我查询这个:
SELECT DISTINCT CategoryId
FROM Products
它可以完美地检索表 Products 中的不同类别。
但是当我尝试从同一查询中的表 Categories 中获取相应的 CategoryName 时:
SELECT DISTINCT p.CategoryId, c.CategoryName
FROM Products p
INNER JOIN Categories c
ON p.CategoryId = c.CategoryId
它会检索更多应该“应该”的行。
要清楚,我只需要在同一个结果集中知道表格产品中不同类别的名称是什么。这可能吗?
架构:
CREATE TABLE [dbo].[Categories](
[CategoryId] [int] NOT NULL,
[CategoryName] [nvarchar](75) NOT NULL,
[dbts] [timestamp] NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Products](
[ProductId] [int] NOT NULL,
[Year] [nvarchar](4) NOT NULL,
[SupplyId] [nvarchar](15) NULL,
[SupplyName] [nvarchar](80) NULL,
[CategoryId] [int] NULL,
[cdate] [datetime] NULL,
[mdate] [datetime] NULL
CONSTRAINT [PK_products] 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]