SQL查询(pl / sql)用于复杂的逻辑

时间:2011-01-20 14:51:09

标签: sql mysql sql-server oracle plsql

我一直坚持这个问题。首先,我将解释表结构

我的系统中有四个表

Columns: 
      DomainID (primary key)
      DomainName

 Data:
        DomainID  DomainName
      1000        Google.com
      2000        mySql.com

网页

 Columns:
      WebpageID   (primary key)
      WebpageName
      DomainID   (FK from domain table)
 Data:
    5001   SearchPage.html   1000
    5002   Welcome.html      1000
    5003   ContactUs.htm     1000
    5004   AboutUs.html      1000

PluginType (可添加到网页的PluginsType)                 此表列出了每个域可用的插件类型

  Columns: 
         PluginType     (primary key)
         DomainID       (primary key)
         PluginTypeName  

   Data
        PluginTypeID        DomainID        PluginTypeName
         8000                 1000             searchButton
         8001                 1000             DropDownMenu
         8002                 1000             InteractiveForm
         8003                 1000             loginForm
         8004                 1000             LogoutForm

插件:域中的每个网页都可以使用任意数量的插件。

      Columns:
             PluginID     (primary key)
             WebpageID    (FK from webpage table)
             pluginTypeID (FK from plugintype table)

      Data:
         pluginID        WebpageID(Name)         PluginTypeID
          10001             5001(SearchPage.html)   8000(SearchButton)
          10002             5001(SearchPage.html)   8001(DropDownMenu)
          10003             5002 (Welcome.html)     8000 (SearchButton)
          10004             5002 (Welcome.html)     8001 (DropDownMenu)
          10005             5002 (Welcome.html)     8004 (lotoutform)  
          10006             5003 (ContactUs.htm)    8003 (loginForm)
          10007             5004 (AboutUs.htm)      8002 (loginForm)

现在我想要的是,给定一个域名ID,我想要所有可用的网页插件列表,这样插件不会在多个网页中重复。 换句话说,每个插件的网页插件组合,以便插件不会在多个网页中重复。

因此对于域名1000(google.com)

我想要的结果是

          5002 (Welcome.html)     8000 (SearchButton)
          5002 (Welcome.html)     8001 (DropDownMenu)
          5002 (Welcome.html)     8004 (lotoutform)
          5003 (ContactUs.htm)    8003 (loginForm)

我只选择了5002和5003个网页,因为它们包含了域名1000(google.com)的所有插件。 还有一件事,最好选择只有一个插件的网页。但我有兴趣找到没有这种偏好的解决方案,以后也许我可以改进解决方案。

3 个答案:

答案 0 :(得分:2)

你可以从另一个角度看一下,因为每个插件类型返回一行,你需要一个域上的所有插件类型,以及一个插件类型的示例页面,好吧,插入-in,类似于:

Select PluginTypeName, 
  (select top 1 WebpageName 
    from WebPage w
      inner join Plugin p on p.WebpageID = w.WebpageID
    where p.pluginTypeID = pt.pluginTypeID) as SampleWebPage
From PluginTypes pt

这将返回类似:

searchButton    SearchPage.html
DropDownMenu    SearchPage.html
InteractiveForm null
loginForm       ContactUs.html
lotoutform      Welcome.html             

答案 1 :(得分:1)

SELECT  PluginTypeId,
        (
        SELECT  webpageID
        FROM    plugin pi
        WHERE   pi.pluginTypeId = pd.pluginTypeId
                AND webPageID IN
                (
                SELECT  WebPageID
                FROM    WebPage
                WHERE   DomainId = 1000
                )
        ORDER BY
                (
                SELECT  COUNT(*)
                FROM    plugin pc
                WHERE   pc.webpageId = pi.webpageId
                )
        LIMIT 1
        ) AS WebPageId
FROM    Plugin pd
WHERE   WebPageId IS NOT NULL

答案 2 :(得分:0)

以上结果(由SWeko提交)应该有效,我正在尝试,可能会反复给出结果。此外,它按域分组结果......以下是修改:

Select DISTINCT DomainID, pt.PluginTypeName, 
  (select top 1 WebpageName 
    from WebPage w
      inner join Plugin p on p.WebpageID = w.WebpageID
    where p.pluginTypeID = pt.pluginTypeID) as SampleWebPage
From PluginTypes pt
GROUP BY DomainID, pt.PluginTypeName