【问题标题】:Implementing a Junction Table实现连接表
【发布时间】:2016-10-28 23:14:33
【问题描述】:

当前版本:

  • 表 (BND_Listing) 作为 A 包含商店位置信息等...

  • 表 (BND_ListingCategories) 作为 B 包含要对 A 进行分类的类别

我正在使用查询字符串在我的网站上创建一个“过滤器”功能,该功能根据用户选择对 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  

我在网站上有一个表单,它基于三个选择框将城市/州/国家/地区值传递到查询字符串中,上面的查询是“正在侦听”以填充我的网格。

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

我已经阅读了 Junction Tables/Many to Many 关系,并认为这是我需要走的路。我了解基本概念,但我无法更新填充网格的 SELECT 语句以考虑我已将表 (BND_Junction) 命名为 C 的连接表。

我真的很感谢你们中的任何一个例子,说明我可以如何操纵我的 select 语句以涉及表 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  

【问题讨论】:

  • 您是否意识到'[querystring:filter-Category]' = 'All' 总是评估为FALSE?在布尔逻辑中,X OR FALSE 等价于X。我不明白你为什么要把这个比较添加到你的 where 子句中......你能启发我们吗?您是否在执行查询之前替换了这些表达式?
  • [querystring:filter-category]' 是一个令牌,它会被我的 Web 应用程序替换,具体取决于用户选择的类别

标签: sql-server tsql junction-table


【解决方案1】:
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  

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2020-10-05
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多