从 SELECT 查看 returns 个不同的结果
View returns different results from its SELECT
我有一个 SQL 观点认为 returns 结果比里面的 select 少,我不知道为什么?
SQL查看代码:
SELECT cs.customer_id, cs.store_id, cs.join_date, c.name, c.email, c.phone, cs.points,
COALESCE (aph.added_point, 0) AS added_point,
COALESCE (aph.spended_cash, 0) AS spended_cash
FROM dbo.customer_store AS cs
INNER JOIN dbo.customer AS c
ON cs.customer_id = c.id
LEFT OUTER JOIN dbo.added_points_history AS aph
ON cs.customer_id = aph.customer_id AND cs.store_id = aph.store_id
上面查询了returns26行,但是我select从视图中看到它returns只有7行!
创建视图的代码:
--Set the options to support indexed views.
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
--Create view with schemabinding.
IF OBJECT_ID ('dbo.customers_store_with_points', 'view') IS NOT NULL
DROP VIEW dbo.customers_store_with_points ;
GO
CREATE VIEW dbo.customers_store_with_points
WITH SCHEMABINDING
AS
SELECT cs.customer_id, cs.store_id, cs.join_date, c.name, c.email, c.phone, cs.points,
COALESCE (aph.added_point, 0) AS added_point,
COALESCE (aph.spended_cash, 0) AS spended_cash
FROM dbo.customer_store AS cs
INNER JOIN dbo.customer AS c
ON cs.customer_id = c.id
LEFT OUTER JOIN dbo.added_points_history AS aph
ON cs.customer_id = aph.customer_id AND cs.store_id = aph.store_id
GO
--Create an index on the view.
CREATE UNIQUE CLUSTERED INDEX IDX_customerId_storeId
ON dbo.customers_store_with_points (customer_id, store_id);
GO
从视图获取结果的select语句:
SELECT TOP 1000 *
FROM [dbo].[customers_store_with_points]
order by store_id
具有相同数据源的相同查询returns相同的结果。
要么你查询的数据库不一样,要么你的查询不一样。
我有一个 SQL 观点认为 returns 结果比里面的 select 少,我不知道为什么?
SQL查看代码:
SELECT cs.customer_id, cs.store_id, cs.join_date, c.name, c.email, c.phone, cs.points,
COALESCE (aph.added_point, 0) AS added_point,
COALESCE (aph.spended_cash, 0) AS spended_cash
FROM dbo.customer_store AS cs
INNER JOIN dbo.customer AS c
ON cs.customer_id = c.id
LEFT OUTER JOIN dbo.added_points_history AS aph
ON cs.customer_id = aph.customer_id AND cs.store_id = aph.store_id
上面查询了returns26行,但是我select从视图中看到它returns只有7行!
创建视图的代码:
--Set the options to support indexed views.
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
--Create view with schemabinding.
IF OBJECT_ID ('dbo.customers_store_with_points', 'view') IS NOT NULL
DROP VIEW dbo.customers_store_with_points ;
GO
CREATE VIEW dbo.customers_store_with_points
WITH SCHEMABINDING
AS
SELECT cs.customer_id, cs.store_id, cs.join_date, c.name, c.email, c.phone, cs.points,
COALESCE (aph.added_point, 0) AS added_point,
COALESCE (aph.spended_cash, 0) AS spended_cash
FROM dbo.customer_store AS cs
INNER JOIN dbo.customer AS c
ON cs.customer_id = c.id
LEFT OUTER JOIN dbo.added_points_history AS aph
ON cs.customer_id = aph.customer_id AND cs.store_id = aph.store_id
GO
--Create an index on the view.
CREATE UNIQUE CLUSTERED INDEX IDX_customerId_storeId
ON dbo.customers_store_with_points (customer_id, store_id);
GO
从视图获取结果的select语句:
SELECT TOP 1000 *
FROM [dbo].[customers_store_with_points]
order by store_id
具有相同数据源的相同查询returns相同的结果。
要么你查询的数据库不一样,要么你的查询不一样。