Combining 2 queries - getting column names in one and using results in another query

时间:2015-06-25 19:10:09

标签: sql ms-access select

Building my first Microsoft Access SQL queries. That should not be this hard! I have 2 tables: A user belonging to GroupA logged in. I want to show him only those Data table rows and columns which GroupA is assigned to, like this: +--------+--------+--------+ | Group | Data3 | Data4 | +--------+--------+--------+ | GroupA | 9 | 4 | | GroupA | 1 | 5 | +--------+--------+--------+ I tried this silly option: SELECT (select Data from AccessRights where GroupA = "y") FROM Data WHERE Data.Group = "GroupA";

4 个答案:

答案 0 :(得分:4)

最好只是转动数据表并添加一个名为data的列。对访问权限也这样做。

您的数据表看起来像这样:

Group, Data, Value
Groupa,Data1,1
Groupb,Data2,7
...

像这样的AccessRights:

Data, Group, Valid
Data1, GroupA, Y
Data2, GroupA, N

然后您可以将两个表连接在一起并根据需要进行过滤。

Select * 
FROM Data D 
  JOIN AccessRights A 
     on D.data = A.data and D.Group = A.Group
WHERE A.Valid = 'Y' 
      and D.Group = 'GroupA'

答案 1 :(得分:3)

我使用此查询:

SELECT 
    Data.[Group], 
    IIf((SELECT GroupA FROM AccessRights WHERE Data = "Data1")="y",[Data1],Null) AS Data_1, 
    IIf((SELECT GroupA FROM AccessRights WHERE Data = "Data2")="y",[Data2],Null) AS Data_2, 
    IIf((SELECT GroupA FROM AccessRights WHERE Data = "Data3")="y",[Data3],Null) AS Data_3, 
    IIf((SELECT GroupA FROM AccessRights WHERE Data = "Data4")="y",[Data4],Null) AS Data_4
FROM 
    Data
WHERE 
    ((Data.[Group])="GroupA");

对于这个结果:

Group   | Data_1 | Data_2 | Data_3 | Data_4
--------+--------+--------+--------+--------
GroupA  |        |        | 9      | 4
GroupA  |        |        | 1      | 5

我只是隐藏Data1Data2的值。

如果你真的想要隐藏你的列,你需要使用VBA我创建一个VBA函数,它将根据你的组提供你的最终查询字符串:

Function myQuery(groupName As String) As String
    Dim strResult As String
    Dim rs As Recordset
    Dim i As Integer

    strResult = "SELECT [DATA].[Group]"

    Set rs = CurrentDb.OpenRecordset("SELECT [Data], [" & groupName & "] FROM AccessRights WHERE [" & groupName & "] = ""y""")

    For i = 0 To rs.RecordCount
        strResult = strResult & "," & rs.Fields("Data").Value
        rs.MoveNext
    Next i

    strResult = strResult & " FROM [Data] WHERE ((Data.[Group])=""" & groupName & """)"

    myQuery = strResult
End Function

例如; myQuery("GroupA")将是

SELECT [DATA].[Group],Data3,Data4 FROM [Data] WHERE ((Data.[Group])="GroupA")

答案 2 :(得分:1)

@ZygD,这是架构:

USER
user_id int primary key auto_increment
user_name varchar(100)
password varchar(100)

GROUP
group_id int primary key auto_increment
group_name varchar(100)

DATA
data_id int primary key auto_increment 
data_name varchar(100)

USER_GROUP
user_id int
group_id int


GROUP_DATA
group_id
data_id

我会解释。首先,您要定义"对象类型"。你有一个USER,GROUP和你所谓的DATA。除了使用另一个单词而不是DATA之外,这可能是一个好主意。使用ITEM甚至DATAITEM之类的东西。对于这个例子,我将使用DATA。好的,所以这些表中的每一个都有一个整数值作为它的主键。主键是表中记录的唯一标识符,它会自动递增。您可以在Access中进行设置。

现在你有三个对象类型表,你需要所谓的"连接表"描述"对象类型"之间的关系。表。 USER_GROUP表表示用户可以属于一个或多个组。例如,如果用户1同时属于组1和组2,那么您将在USER_GROUP表中有两条记录来描述这些关系。第一行是1,1,第二行是1,2。

GROUP_DATA表描述了GROUP和DATA之间的关系。例如,组1可以访问数据2和数据3.同样,您将在GROUP_DATA表中有两行来描述这些关系。第一行是1,2,第二行是1,3。

现在,因为用户1属于第1组,所以用户1可以访问数据2和3.然后您的SQL变得简化:

// Authenticate the user with user_name and password:
select @user_id = a.user_id from user a where a.user_name = @user_name and a.password = @password

// Get DATA by user_id
select c.data_id, c.data_name from user a join group b on a.user_id = b.user_id join data c on b.data_id = c.data_id where a.user_id = @user_id

答案 3 :(得分:0)

好的最后,这是您需要的结果。 这个解决方案的好处是你不需要运行额外的脚本,也只需将组名作为参数传递,它将只返回你需要的列。请享用。 :)

declare @aa varchar (200) = ''
declare @sql varchar(500) = ''
declare @groupinfo varchar(100) = 'GroupA'

Select @aa = coalesce (case when @aa = '' then Data else @aa + ',' + Data end ,'')
  from [AccessRights] where GroupA = 'y'

Set @sql = 'Select [Group],' +  @aa + ' from Data where [Group] = ' + '''' + @groupinfo + ''''
Exec(@sql)

+--------+--------+--------+
| Group  |  Data3 | Data4  |
+--------+--------+--------+
| GroupA |   9    |   4    | 
| GroupA |   1    |   5    |
+--------+--------+--------+