简体   繁体   English

SQL:根据另一个表中的列值选择列

[英]SQL: Selecting columns based on column value from another table

I have the following tables: 我有以下表格:

UserPrivileges: 
+--------+------+------+------+
| UserID | Col1 | Col2 | Col3 |
+--------+------+------+------+
|      1 |    0 |    1 |    1 |
|      2 |    0 |    0 |    1 |
|      3 |    1 |    0 |    0 |
|      4 |    1 |    1 |    0 |
+--------+------+------+------+

Data:
+--------+------+------+------+
| DataID | Col1 | Col2 | Col3 |
+--------+------+------+------+
|      1 | A    | B    | C    |
|      2 | D    | E    | F    |
|      3 | G    | H    | I    |
|      4 | J    | K    | L    |
+--------+------+------+------+

My question at its simplest form doesn't has anything to do with the Data table but I just explain it anyways so that I might be doing it the wrong way. 我最简单的问题与Data表没有任何关系,但我只是解释它,以便我可能以错误的方式做到这一点。

How would I select the Column names from UserPrivileges based on the value ? 如何根据值从UserPrivileges中选择列名? So that I can use the result in another query to select only those columns. 这样我就可以在另一个查询中使用结果来只选择那些列。

Something along these lines: 这些方面的东西:

SELECT (COLUMNS_NAME_QUERY_FROM_UserPrivileges(UserID='#')) WHERE DataID = '#' FROM Data

Or I don't mind a better way to manage user Privileges for specific columns. 或者我不介意管理特定列的用户权限的更好方法。

The answer depends upon your requirements for the result. 答案取决于您对结果的要求。 Do you require a result with a consistent set of columns, regardless of user privs? 无论用户权限如何,您是否需要具有一致列集的结果? If so, you could set the disallowed values to null (or some other special value) using a IF clause, eg, 如果是这样,您可以使用IF子句将不允许的值设置为null(或其他一些特殊值),例如,

SELECT IF (p.col1 = 0 THEN NULL ELSE d.col1) AS col1, 
       IF (p.col2 = 0 THEN NULL ELSE d.col2) AS col2,
       IF (p.col3 = 0 THEN NULL ELSE d.col3) AS col3
FROM Data d, 
     UserPrivileges p
WHERE p.userId = '#' 
  AND d.DataId = '#'

Of course, the "special value" could be a problem, since you need a value that would never appear in the data. 当然,“特殊值”可能是个问题,因为您需要一个永远不会出现在数据中的值。 If you needed to know that difference between a null because the real value is null vs. null because it is a prohibited column then you can't use null. 如果您需要知道null之间的差异,因为实际值为null而不是null,因为它是禁止列,那么您不能使用null。

Another approach would have you simple include the privilege indicator for each column appear in the result, and let your business logic use that to determine which values are visible to the user. 另一种方法是让您简单地在结果中包含每列的权限指示符,并让您的业务逻辑使用它来确定哪些值对用户可见。

A very different approach would have the result set to contain only the allowed columns. 一种非常不同的方法是将结果集仅包含允许的列。 In this case you'll need to build your sql statement dynamically. 在这种情况下,您需要动态构建sql语句。 I don't know if you are doing this in a stored procedure or in a host language, but the basic idea is something like this: 我不知道你是在存储过程中还是在宿主语言中这样做,但基本的想法是这样的:

string sqlCmd = "SELECT " 
    + (SELECT (FIELDS_NAME_QUERY(UserID='#') 
       FROM USER_PRIVILEGES 
       WHERE userid='#') 
    + FROM data d 
execute sqlCmd

"execute" meaning whatever you have available to execute a string as a sql command. “执行”意味着您可以执行字符串作为sql命令。


more after clarification by OP: 经过OP澄清后更多:

Ok, you need sql function that returns a string that looks like "colname1, colname2, ...". 好的,你需要sql函数返回一个看起来像“colname1,colname2,...”的字符串。 The following resembles what it would look like in sql server. 以下类似于sql server中的样子。 syntax 句法

create function   
FIELDS_NAME_QUERY (@userid int)  
begin  
select col1, col2, col3... INTO @col1priv, @col2priv, @col3priv FROM userPrivileges WHERE UserId = @UserId  
declare @result varhcar(60)  
set @result = ''  
if (@col1priv = 1) @result = 'col1'  
if (@col2priv = 1) @result = @result + ' ,col2'  
if (@col3priv = 1) @result = @result + ' ,col3'  
return @result  
end

have not tried it, but something like this should work 没有尝试过,但这样的事情应该有效

SELECT (SHOW COLUMNS FROM table WHERE expr) FROM data WHERE DataID = '#'

Check this post for details - How can I get column names from a table in Oracle? 查看此帖子了解详细信息 - 如何从Oracle中的表中获取列名?

Let us know how you solve this... 让我们知道你如何解决这个问题......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM