简体   繁体   English

SQL-从表中选择列,其中另一个表的列值等于列名列表

[英]SQL - Select Columns from table where another table column value equals column name list

seen a lot of similar posts about parsing data based on values but nothing quite what I want to get the result column from one table that contains the column names for another table. 见过很多关于基于值解析数据的类似文章,但我所想的都不是我想从一个包含另一个表的列名的表中获取结果列。 For example: 例如:

 Select [col1], [col2]
 from Table1
 where (select changes as (col#)
        from table2)

The col# is a list of the column names I want from Table1. col#是我想要从Table1中获得的列名的列表。 Table2 changes column is a comma separated list. 表2更改列是用逗号分隔的列表。 I wanted to figure a way to be able to get each column from Table1 that's in the list from table2. 我想找到一种方法,能够从Table2列表中的Table1中获取每一列。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

You'll have to create the query on-the-fly. 您必须即时创建查询。 You can either do this by: 您可以通过以下方式执行此操作:

  1. First fetching the data from the changes column into your calling application, then assembling a SQL query string from that data and execute it, or 首先,将来自changes列的数据提取到调用的应用程序中,然后从该数据中组装一个SQL查询字符串并执行它,或者
  2. Creating a dynamic statement within a stored procedure and EXEC ing it (or whatever your RDBMS's equivalent statement is). 在存储过程中创建动态语句并将其EXEC (或与RDBMS等效的任何语句)。

Do be aware though that if the data in changes ever comes from user input, you are in for a world of hurt. 但是请注意,如果changes的数据曾经来自用户输入,那么您将遭受changes的伤害。

If you have control over the schema I would suggest to avoid this way of storing data. 如果您可以控制架构,建议不要使用这种存储数据的方式。 In general, comma-separated-lists are just unwieldy and fundamentally go against what a database is designed to do. 通常,用逗号分隔的列表很笨拙,并且从根本上违背了数据库的设计意图。 A change-table structure with one Boolean value for each column that could change (id, col1_changed, col2_changed) or with a single row for each changed column (id, changed_col_name) would be much easier to use in a normal query. 在普通查询中,使用可更改的每一列具有一个布尔值(id, col1_changed, col2_changed)或每个更改的列具有一行(id, changed_col_name)的更改表结构会容易得多。

暂无
暂无

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

相关问题 Oracle-SQL查询以从另一个表中的列返回值,其中值等于另一个列 - Oracle - SQL Query to return a value from a column in another table where value equals a different column 从一个表中选择所有列,从另一个表中选择一个列,其中列等于变量 - Select all columns from one table and one from another where column equals variable 表中的 Select 行,其中 id 等于另一个表中的另一个 id,并对结果列中的值求和 - Select rows in a table where id is equals to another id in another table and sum values in column from the result SQL选择一个表中的所有列和另一个表上另一列的最大值 - SQL Select all columns from one table and Max value of another column on another table 如何从多个表中选择数据,其中表名是另一个表的列中的值? - How to select data from multiple tables where table name is value from a column of another table? Oracle SQL:从列名来自另一个表的列中获取值 - Oracle SQL: Get value from column where column name comes from a another table 在表中选择all表,其中表1中的列存在于另一个表中,第二列等于第二个表中的变量 - Select all in table where a column in table 1 exists in another table and a second column equals a variable in second table 在SQL中联接表,其中新列名基于另一列的联接表值 - Joining tables in SQL where new column name is based on joined table value from another column Select 值来自表名,其中表名存储为使用动态 SQL 的其他表中的列中的值? - Select value from a table name where table name is stored as a value in column in some other table using dynamic SQL? 如何通过从 SQL Server 中的另一个表中选择列名和值将行插入表中 - How to insert row into a table by select column name and value from another table in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM