我有数据存储我的日常内容选择。这对于每一天都是不同的。
当我选择超过一天时,我需要退货,所有天都一样。
我尝试过这样的事情:
SELECT * FROM
(
SELECT
( -- For count
SELECT COUNT(recipeId) AS [Count]
FROM DailyContentDish
WHERE
(
(weekDayId=@mon OR @mon IS NULL) OR
(weekDayId=@tue OR @tue IS NULL) OR
(weekDayId=@wed OR @wed IS NULL) OR
(weekDayId=@thu OR @thu IS NULL) OR
(weekDayId=@fri OR @fri IS NULL) OR
(weekDayId=@sat OR @sat IS NULL) OR
(weekDayId=@sun OR @sun IS NULL)
)
GROUP BY DailyContentDish.recipeId
) AS cnt,
-- End for count
DailyContentDish.dailyContentDishId,
DailyContentDish.recipeId AS Rec,
title,
150 AS calories,
defaultSmallImagePath,
activePreparationTime + passivePreparationTime AS overallPreparationTime,
CAST(
CASE WHEN EXISTS
(
SELECT DailyContentDishFavourite.dailyContentDishFavouriteId
FROM DailyContentDishFavourite
WHERE DailyContentDishFavourite.dailyContentDishId = DailyContentDish.dailyContentDishId
)
THEN 1
ELSE 0
END
AS BIT) AS isFavouriteWhenGeneratingMenu
FROM DailyContentDish
LEFT OUTER JOIN
RecipesTranslations ON RecipesTranslations.recipeId = DailyContentDish.recipeId
LEFT OUTER JOIN
RecipeAdditionalInformation ON RecipeAdditionalInformation.recipeId = DailyContentDish.recipeId
WHERE
isEnabled = 1 AND
mealId=@mealId AND
(
weekDayId=@mon OR
weekDayId=@tue OR
weekDayId=@wed OR
weekDayId=@thu OR
weekDayId=@fri OR
weekDayId=@sat OR
weekDayId=@sun
)
) p
WHERE p.cnt = @daysCount
问题是,应该返回 count 的嵌套选择会为所有行返回它,而不仅仅是一个条目(即每一行)。由于带有 recipeId 的条目输入的次数更多,因此我想知道它们输入了多少次。
SELECT
( -- For count
SELECT COUNT(recipeId) AS [Count]
FROM DailyContentDish
WHERE
(
(weekDayId=@mon OR @mon IS NULL) OR
(weekDayId=@tue OR @tue IS NULL) OR
(weekDayId=@wed OR @wed IS NULL) OR
(weekDayId=@thu OR @thu IS NULL) OR
(weekDayId=@fri OR @fri IS NULL) OR
(weekDayId=@sat OR @sat IS NULL) OR
(weekDayId=@sun OR @sun IS NULL)
) AND Something should be here (I guess)
GROUP BY DailyContentDish.recipeId
) AS cnt,
-- End for count
这部分应该为我选择的每一行返回 COUNT 个条目 - 但它会返回所有条目的 COUNT 个。
或者我应该采取不同的方式。非常感谢任何提示。
我正在使用 MS SQL 服务器 2008
编辑:这是整个存储过程:
@userId int,
@languageId int,
@mealId int,
@mon int,
@tue int,
@wed int,
@thu int,
@fri int,
@sat int,
@sun int,
@orderBy nvarchar(2),
@pageSize int,
@startRowIndex int
AS
BEGIN
SET NOCOUNT ON;
DECLARE @daysCount int
SET @daysCount = 0
IF (@mon IS NOT NULL)
BEGIN
SET @daysCount = @daysCount+1
END
IF (@tue IS NOT NULL)
BEGIN
SET @daysCount = @daysCount+1
END
IF (@wed IS NOT NULL)
BEGIN
SET @daysCount = @daysCount+1
END
IF (@thu IS NOT NULL)
BEGIN
SET @daysCount = @daysCount+1
END
IF (@fri IS NOT NULL)
BEGIN
SET @daysCount = @daysCount+1
END
IF (@sat IS NOT NULL)
BEGIN
SET @daysCount = @daysCount+1
END
IF (@sun IS NOT NULL)
BEGIN
SET @daysCount = @daysCount+1
END
-- Insert statements for procedure here
SELECT *
FROM (
SELECT
(
SELECT [Count] = COUNT(recipeId)
FROM dbo.DailyContentDish d
WHERE
(
ISNULL(@mon, weekDayId) = weekDayId OR
ISNULL(@tue, weekDayId) = weekDayId OR
ISNULL(@wed, weekDayId) = weekDayId OR
ISNULL(@thu, weekDayId) = weekDayId OR
ISNULL(@fri, weekDayId) = weekDayId OR
ISNULL(@sat, weekDayId) = weekDayId OR
ISNULL(@sun, weekDayId) = weekDayId
)
GROUP BY d.recipeId
) AS cnt,
d.dailyContentDishId,
d.recipeId AS Rec,
title,
150 AS calories,
defaultSmallImagePath,
activePreparationTime + passivePreparationTime AS overallPreparationTime,
CASE WHEN d2.dailyContentDishId IS NULL THEN 1 ELSE 0 END AS isFavouriteWhenGeneratingMenu
FROM dbo.DailyContentDish d
LEFT JOIN dbo.RecipesTranslations r ON r.recipeId = d.recipeId
LEFT JOIN dbo.RecipeAdditionalInformation t ON t.recipeId = d.recipeId
LEFT JOIN dbo.DailyContentDishFavourite d2 ON d.dailyContentDishId = d2.dailyContentDishId
WHERE isEnabled = 1
AND mealId = @mealId
AND (
weekDayId = @mon OR
weekDayId = @tue OR
weekDayId = @wed OR
weekDayId = @thu OR
weekDayId = @fri OR
weekDayId = @sat OR
weekDayId = @sun
)
) p
WHERE p.cnt = @daysCount
Edit1:我已经上传了图表:
这是示例数据(对于膳食 ID = 1,这也在表格中选择),说明: - ID 为 125 的食谱将在星期一(weekDayId = 1)和星期六(weekDayId = 7)出现。所以,如果我只选择星期一或者如果我只选择星期六或者如果我选择星期一和星期六,则必须返回这个食谱。如果我还选择了其他任何一天,则不会返回 ithis 记录。- 选择第 1、6 和 7 周(周一、周六、周日)时,必须返回 ID 为 105 的食谱。同上,如果选择了其他任何一天,则不返回该记录。