带有选择选项的SQL帮助

时间:2011-05-06 09:10:06

标签: sql sql-server tsql

如果我能让问题更容易理解,请告诉我,因为我真的可以帮忙...

我们有一个包含类别和别名的目录,我正在尝试添加创建进入顶级类别的别名子类别的功能,目前我们可以只将别名子类别添加到其他子类别,这不是我们想要的。

例如,我想将子类别'地产代理'添加到主要类别'Home and Garden'和< em>'财务与抵押'。

我只能将'地产代理'添加到其中一个子类别中,因此希望在下拉选择选项中包含顶级类别以及子类别。希望有意义!

代码在下面发布,由2个单独的文件组成,用于处理数据。目前正在请求PGID - 这是父目录GID - 即顶部等级类别。

我们希望别名下拉选择选项不仅显示父类别(PGID)顶级类别下的子类别(GID),还显示PGID顶级类别本身。

E.g。 select选项包含所有级别类别,例如:

  • 家居与园艺(PGID)
  • 地产代理(GID)
  • 家具供应商(GID)
  • 花园中心(GID)等。

不只是:

  • 地产代理(GID)
  • 家具供应商(GID)
  • 花园中心(GID)

如果我能进一步澄清,请告诉我,因为它很简单,但很难解释!

AddAlias.asp

<!--#include virtual="/Admin/FranchiseAdmin/DirectorySetup/Aliases/i_Alias.asp"-->
 <%
  objConn.Open strConn
 %>
 <input type="hidden" **name="PGID" value="<% = Request("PGID") %>"** />
  <select>
  <%
  SQLCommand = "SELECT * FROM Directories WHERE GID='" & CleanSQLText(Request("PGID")) & "'"
  rsTemp.Open SQLCommand, objConn, adOpenStatic, adLockReadOnly
  if rsTemp.RecordCount <> 1 then
  FailSystemEvent "AddAlias:  Failed to load Existing Parent"
  Else
  %>
  <option value="<% = rsTemp("GID") %>"><% = rsTemp("DirectoryName") %></option><%
  End if
  rsTemp.Close
  SQLCommand = "SELECT * FROM Directories WHERE ParentDirectoryGID IS NOT NULL AND GID <> '" & CleanSQLText(Request("PGID")) & "' ORDER BY DirectoryName"
  rsTemp.Open SQLCommand, objConn, adOpenStatic, adLockReadOnly
  While Not rsTemp.EOF
  CreateSelectOption rsTemp("GID"), rsTemp("DirectoryName"), CleanSQLText(Request("PGID"))
  rsTemp.MoveNext
  Wend
  rsTemp.Close
  %>
  </select>
 <%
 objConn.Close
 %>

我在上面的代码ParentDirectoryGID IS NOT NULL中删除了以下内容,现在我现在可以看到顶级的灾难,但由于错误而无法保存

错误是从包含文件i_Alias.asp生成的,并说:发生了错误。目录GID无效

以下是包含文件代码:i_Alias.asp

<%

Dim SQLCommand : SQLCommand = ""
Dim SQLFilter : SQLFilter = ""
Dim objConn : Set objConn = Server.CreateObject("ADODB.Connection")
Dim rsAlias : Set rsAlias = Server.CreateObject("ADODB.RecordSet")
Dim rsTemp : Set rsTemp = Server.CreateObject("ADODB.RecordSet")
Dim strUserError : strUserError = ""

Dim strFranchiseGID
Dim strDirectoryGID

Dim objEditor

Select Case Request.Form("Action")
    Case "AddNew"

        'strFranchiseGID = CleanSQLData(Request.Form("strFranchiseGID"))
        'If strFranchiseGID = "" then strFranchiseGID = Null
        strFranchiseGID = Session("AdminFranchiseGID")

        strDirectoryGID = CleanSQLData(Request.Form("PGID"))
        If strDirectoryGID = "" then 
            strUserError = strUserError & "An error has occured.  The Directory GID is invalid"
        end if

        if strUserError = "" then
            objConn.Open strConn
            SQLCommand = "SELECT TOP 1 * FROM DirectoryAliases"
            rsAlias.Open SQLCommand, objConn, adOpenKeySet, adLockOptimistic
            rsAlias.AddNew
            rsAlias("FranchiseGID") = strFranchiseGID
            rsAlias("DirectoryGID") = strDirectoryGID
            rsAlias.Update
            rsAlias.Close
            objConn.Close
            'Response.Redirect "Default.asp?PGID=" & Request("PGID")
            ' Redirect to the New Parent Folders
            Response.Redirect "Default.asp?PGID=" & strDirectoryGID
        End if
    Case "Update"
        if Not Session("Role_Franchise_ManageDirectory") = True then
            AccessDenied
        End if
        objConn.Open strConn
        SQLCommand = "SELECT * FROM DirectoryAliases WHERE GID = '" & CleanSQLData(Request.Form("GID")) & "'"
        rsAlias.Open SQLCommand, objConn, adOpenKeySet, adLockOptimistic
        if rsAlias.RecordCount <> 1 then
            FailSystemEvent "EditAlias Update failed - could not load unique record"
            rsAlias.Close
            objConn.Close
            Response.End
        End if
        'strFranchiseGID = CleanSQLData(Request.Form("strFranchiseGID"))
        'If strFranchiseGID = "" then strFranchiseGID = Null
        strFranchiseGID = Session("AdminFranchiseGID")
        rsAlias("FranchiseGID") = strFranchiseGID

        strDirectoryGID = CleanSQLData(Request.Form("PGID"))
        If strDirectoryGID = "" then 
            strUserError = strUserError & "An error occured.  The Directory GID is invalid"
        else
            rsAlias("DirectoryGID") = strDirectoryGID
        End if
        rsAlias.Update
        rsAlias.Close
        objConn.Close
        if strUserError = "" then
            Response.Redirect "Default.asp?PGID=" & Request("PGID")
        end if
    Case "Delete"
        if Not Session("Role_Franchise_ManageDirectory") = True then
            AccessDenied
        End if
        SQLCommand = "DELETE DirectoryAliases WHERE GID = '" & CleanSQLText(Request.Form("GID")) & "'"
        objConn.Open strConn
        objConn.Execute SQLCommand
        objConn.Close
End Select
%>

希望有人可以提供帮助!

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解你的问题,但是你可以用UNION来做到这一点。

假设您的表A包含列desc和PGID,表B包含cols desc,GID和PGID

select * from
(
select desc, GID as X from A
union all
select desc, PGID as X from B
) both_tables_unioned
order by X, desc

所以如果表A包含:

desc     GID
----     ---
A        1
B        2

和B包含:

desc     PGID
----     ----
AA       1
AB       1
BA       2
BB       2

你应该得到

A       1 
AA      1 
AB      1 
B       1 
BA      1 
BB      1
结果

答案 1 :(得分:0)

你能更多地描述表结构吗?

它看起来是一个父子关系 所以: 目录表 有主键GID, 将外键ParentDirectoryGID添加到directory.GID

示例数据:

  • GID 9 - Home&amp; Garden(GID)ParentDirectoryGID = NULL
  • GID 10 - 地产代理(GID)ParentDirectoryGID = 9
  • GID 11 - 家具供应商(GID)ParentDirectoryGID = 9
  • GID 12 - Garden Centers(GID)等.DarentDirectoryGID = 9

您想要做的是填充由父母及其所有后代组成的下拉菜单(儿童的孩子怎么办?)

如果正确,您可能希望查看oracle Connect By Prior中的分层查询(但我不知道您使用的是什么数据库引擎)