实施联结 Table

Implementing a Junction Table

当前版本:

我正在使用查询字符串在我的网站上创建一个 "Filter" 函数,该函数根据用户 selection 对 A 进行排序。我正在使用以下 select 语句来填充我网站上的网格:

SELECT *
FROM   bnd_listing_testing
       RIGHT JOIN bnd_listingcategories
               ON bnd_listing_testing.catid = bnd_listingcategories.catid
WHERE  ( categoryname = '[querystring:filter-Category]'
          OR '[querystring:filter-Category]' = 'All' )
       AND ( city = '[querystring:filter-City]'
              OR '[querystring:filter-City]' = 'All' )
       AND ( region = '[querystring:filter-State]'
              OR '[querystring:filter-State]' = 'All' )
       AND ( country = '[querystring:filter-Country]'
              OR '[querystring:filter-Country]' = 'All' )
       AND ISNULL(company, '') <> ''
ORDER  BY company ASC  

我在网站上有一个表单,它基于三个 select 框将 City/State/Country 值传递到查询字符串中,上面的查询 "Listening" 用于填充我的网格.

现在一切正常,但是我需要将多个类别归因于 table A 中的任何一条记录,该记录当前只是一个具有一个值的列。

我已经阅读了 Junction Tables/Many 到 Many relationships 的文章,认为这是我需要走的路。我了解基本概念,但我无法更新我的 SELECT 语句,该语句正在填充我的网格以考虑到我命名为 Table (BND_Junction) 的 Junction table作为 C.

我真的很感激你们中的任何一个例子,关于我如何操纵我的 select 语句以涉及 table C,但仍然发挥相同的作用。

如果有任何问题需要澄清,请告诉我。


更新

我已经尝试了以下有效的查询,但为简单起见删除了查询字符串过滤器部分。

SELECT DISTINCT *
FROM   bnd_listingjunction
       JOIN bnd_listing
         ON bnd_listing.catid = bnd_listingjunction.catid
       JOIN bnd_listingcategories
         ON bnd_listingcategories.catid = bnd_listingjunction.catid  
SELECT *
FROM   bnd_listingjunction
       JOIN bnd_listing_testing
         ON bnd_listing_testing.lid = bnd_listingjunction.junc_lid
       JOIN bnd_listingcategories
         ON bnd_listingcategories.catid = bnd_listingjunction.junc_catid
WHERE  ( categoryname = '[querystring:filter-Category]'
          OR '[querystring:filter-Category]' = 'All' )
       AND ( city = '[querystring:filter-City]'
              OR '[querystring:filter-City]' = 'All' )
       AND ( region = '[querystring:filter-State]'
              OR '[querystring:filter-State]' = 'All' )
       AND ( country = '[querystring:filter-Country]'
              OR '[querystring:filter-Country]' = 'All' )
       AND ISNULL(company, '') <> ''
ORDER  BY company ASC