如何为两个连接表应用Containstable?

时间:2010-06-16 08:19:08

标签: sql sql-server-2008

产品表

pid modelnumber
1      a
2      b
3      c

ProductTransation

pid  name description...
1   ball   ball
2   bat     cricket bat

我在产品表中为Modelnumber创建了fullText。

同名和& productTransaction table中的描述。

现在,如果我搜索modelnumber或name

,我想加入此表

结果应该是

pid  name  modelnumber 
1     ball  a

1 个答案:

答案 0 :(得分:0)

我目前做类似以下的事情:

SELECT
        PD.pid,
        PD.name,
        PD.modelnumber
    FROM (
        SELECT
                (
                    COALESCE(ctP.[RANK], -1)
                    +
                    COALESCE(ctPT.[RANK], -1)
                    ) AS [RANK],
                P.pid,
                PT.name,
                P.modelnumber
            FROM Product AS P
            INNER JOIN ProductTranslation AS PT
                ON PT.pid = P.pid
            OUTER APPLY (
                SELECT TOP 1
                            ctP.[KEY],
                            ctP.[RANK]
                    FROM CONTAINSTABLE(
                        Product,
                        *,
                        @Query
                        ) AS ctP
                    WHERE ctP.[KEY] = P.pid
                )
            OUTER APPLY (
                SELECT TOP 1
                            ctPT.[KEY],
                            ctPT.[RANK]
                    FROM CONTAINSTABLE(
                        ProductTranslation,
                        *,
                        @Query
                        ) AS ctPT
                    WHERE ctPT.[KEY] = P.pid
                )
            WHERE
                (
                    ctP.[KEY] IS NOT NULL
                    OR
                    ctPT.[KEY] IS NOT NULL
                    )
        ) AS PD
    ORDER BY PD.[RANK] DESC