IF在WHERE子句中的EXISTS

时间:2013-03-30 20:48:05

标签: sql sql-server where exists

这几天我遇到了一个问题。在我的MS SQL数据库中,我有一个文章表(它们的详细信息,如插入日期,插入用户和其他人员)和一个表格,其中包含多种语言的文章正文。我希望文章正文是用户首选语言。但并非所有文章都使用所有语言。所以,很高兴,首先用用户首选的语言搜索文章,如果不存在的话,用第一语言来获取正文。

为此,我使用了一个函数。 WHERE子句是这样的:

WHERE   [tblBody].[Language] = @Language AND
        [tblTitle].[Language] = @Language

但如果它会是这样的话会很好: 选择(如果身体是我的首选语言得到那个身体;否则给我一个(也许我理解))

我希望你明白我想做什么,女巫是我的问题。

提前谢谢!

更新

这是我需要修改的实际查询:

 ALTER FUNCTION [fx_GetNews]
(   
    @MinimumPermission INT,
    @UserID UNIQUEIDENTIFIER,
    @OwnerID  UNIQUEIDENTIFIER = NULL,
    @Title NVARCHAR(250) = NULL,
    @Body VARCHAR(150) = NULL,
    @InsertDateStart DATETIME = NULL,
    @InsertDateEnd DATETIME = NULL,
    @CreatedByUserID UNIQUEIDENTIFIER = NULL,
    @ExpirationDate DATETIME = NULL,
    @Language VARCHAR(150)
)
RETURNS @News TABLE 
(
    [Id] UNIQUEIDENTIFIER
    ,[OwnerID] UNIQUEIDENTIFIER
    ,[Title] NVARCHAR(250)
    ,[TitlePictureUrl] VARCHAR(150)
    ,[Body] NVARCHAR(max)
    ,[Visible] BIT
    ,[InsertDate] DATETIME
    ,[CreatedByUserId] UNIQUEIDENTIFIER
    ,[ModifiedDate] DATETIME
    ,[ModifiedByUserId] UNIQUEIDENTIFIER
    ,[ModifiedByPerson] VARCHAR(250)
    ,[ExpirationDate] DATETIME
    ,[CreatedByPerson] VARCHAR(250)
    ,[Permission] INT
)
AS
BEGIN
    INSERT INTO @News
           ([Id]
           ,[OwnerID]
           ,[Title]
           ,[TitlePictureUrl]
           ,[Body]
           ,[Visible]
           ,[InsertDate]
           ,[CreatedByUserId]
           ,[ModifiedDate]
           ,[ModifiedByUserId]
           ,[CreatedByPerson]
           ,[ModifiedByPerson]
           ,[ExpirationDate]
           ,[Permission])
        SELECT
                [dbo].[tblNews].[Id]
               ,[dbo].[tblNews].[OwnerID]
               ,CAST([tblTitle].[Text] AS VARCHAR(150))
               ,[dbo].[tblNews].[TitlePictureUrl]
               ,[tblBody].[Text]
               ,[dbo].[tblNews].[Visible]
               ,[dbo].[tblNews].[InsertDate]
               ,[dbo].[tblNews].[CreatedByUserId]
               ,[dbo].[tblNews].[ModifiedDate]
               ,[dbo].[tblNews].[ModifiedByUserId]
               ,[dbo].[tblNews].[CreatedByPerson]
               ,[dbo].[tblNews].[ModifiedByPerson]
               ,[dbo].[tblNews].[ExpirationDate]
               ,[eportofolii].[fx_GetPermissionForObject] (@UserID, [dbo].[tblNews].[ID], 1, 1)
           FROM [dbo].[tblNews] 
                    INNER JOIN [dbo].[tblAdmTranslateText] AS [tblBody] ON
                        [tblBody].[OwnerID] = [dbo].[tblNews].[ID]
                    INNER JOIN [dbo].[tblAdmTranslateText] AS [tblTitle] ON
                        [tblTitle].[OwnerID] = [dbo].[tblNews].[ID]
        WHERE
            [eportofolii].[fx_GetPermissionForObject] (@UserID, [dbo].[tblNews].[ID], 1, 1) >= @MinimumPermission AND
            ([dbo].[tblNews].[OwnerID] = ISNULL(@OwnerID, [dbo].[tblNews].[OwnerID]) OR [dbo].[tblNews].[OwnerID] IS NULL) AND
            [tblTitle].[Text] LIKE '%' + ISNULL(@Title, '') + '%' AND
            [tblBody].[Text] LIKE '%' + ISNULL(@Body, '') + '%' AND
            ([dbo].[tblNews].[InsertDate] BETWEEN ISNULL(@InsertDateStart, ([dbo].[tblNews].[InsertDate] - 7)) AND ISNULL(@InsertDateEnd, [dbo].[tblNews].[InsertDate] + 1)) AND
            [dbo].[tblNews].[CreatedByUserID] = ISNULL(@CreatedByUserID, [dbo].[tblNews].[CreatedByUserID]) AND
            ([dbo].[tblNews].[ExpirationDate] > ISNULL(@ExpirationDate, GETDATE() - 1) OR [dbo].[tblNews].[ExpirationDate] IS NULL) AND
            [tblBody].[UDF_2] = 'Body' AND 
            [tblTitle].[UDF_2] = 'Title' AND 
            [tblBody].[UDF_1] = 'News' AND 
            [tblTitle].[UDF_1] = 'News' AND
            [tblBody].[Language] = @Language AND
            [tblTitle].[Language] = @Language
        ORDER BY [InsertDate] DESC
    RETURN
END

问题出在最后:

[tblBody].[Language] = @Language AND
[tblTitle].[Language] = @Language

谢谢!

4 个答案:

答案 0 :(得分:2)

;WITH x AS 
(
   SELECT b.[Language], /* other columns from b */
     rn = ROW_NUMBER() OVER (PARTITION BY b.article_id_of_some_kind
       ORDER BY CASE WHEN t.[Language] = @Language THEN 1 ELSE 2 END)
   FROM dbo.tblBody AS b
   INNER JOIN dbo.tblLanguage AS l
    ON b.LanguageID = l.LanguageID -- guessing here
)
SELECT /* cols */ FROM x WHERE rn = 1;

如果首选语言不可用,将返回任意语言。您可以通过修改内部ORDER BY来进一步完善它。

答案 1 :(得分:0)

你想做的事情有点复杂。您希望以所需语言(如果存在)返回文章,如果不存在,则返回其他语言。

为此,窗口函数非常有用:

select t.*
from (select blah,
             max(case when [tblBody].[Language] = @Language then 1 else 0 end) over (partition by article) as HasLanguage       
      from whatever
      where [tblTitle].[Language] = @Language
     ) t
where HasLanguage = 1 or [tblBody].[Language] <> @Language 

答案 2 :(得分:0)

select B.*
from bodies as B
where exists (
  select 1 from (
    select b.article, coalesce( u.language, b.language ) as language
    from bodies as b 
    left join users as u
    on b.language = u.language
    and u.user = @user
    where b.article = @article
    and b.default_language = 'TRUE' -- or whatever
  ) as A
  on A.article = B.Article
  and A.language = B.language
)

您必须在两种语言之间进行选择:用户的语言和文章的语言。你可以使用UNIONcoalesce(因为外连接是一种联合),而后者更简单。

答案 3 :(得分:-1)

一种选择是使用MSSQL“COALESCE”功能:

另一种可能是使用CASE块。