Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select columns from one table based on the column names from another table

I have 2 tables, one that contains the final results I need, and another that contains the list of columns I need to select from based on a set level.

For example :

table_levels

level | name      | [selected-columns]
1     | startdate | start_date
1     | end date  | end_date
1     | contract  | contract

So if i do the following

select column from table_levels where level = '1'

Then basically i need to use the column names from this select statement to determine which columns are selected from another statement.

This is what ive tried and of course i know its wrong but gives you an idea of what im trying to do.

select (select [selected-columns] from table_levels where nwlevel = '1')
from table_results

In a way im trying to dynamically build an sql query which can be altered by whichever columns i put in the table_levels table.

This should in theory act the same as the following sql query

select start_date, end_date, contract
from table_results
like image 448
Stew Avatar asked Sep 09 '13 15:09

Stew


People also ask

How can I get column names from another table in SQL?

The System Stored Procedure sp_columns returns Column names with additional information from the Database table in SQL Server. In the following example, the Column names of Employees table of Northwind Database are fetched using the Stored Procedure sp_columns.

How do I select data from another table in SQL?

In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables.

How do I select data from one table to another?

The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.


1 Answers

My previous answer was for mysql. Since the tag has been updated on the question since then, here is the query for sql-server-2008.

Build a list of columns from the values in table_levels, remove the last ,, build a query string to get you the results from table_results, and then execute.

DECLARE @listStr varchar(MAX) = ( select selectColumnName + ',' from table_levels where level = 1 for xml path(''))
DECLARE @query varchar(MAX) = 'SELECT ' + LEFT(@listStr, LEN(@listStr)-1) + ' FROM table_results'
execute(@query)

Demo for sql server


Previous answer. Works for mssql

See demo for mysql

Use GROUP_CONCAT to make a string out of the values in table_levels and then build a query string to get you the results from table_results

SET @listStr = ( SELECT GROUP_CONCAT(selectColumnName) FROM table_levels where level = 1);
SET @query := CONCAT('SELECT ', @listStr, ' FROM table_results');
PREPARE STMT FROM @query;
EXECUTE STMT;
like image 177
Praveen Lobo Avatar answered Sep 18 '22 23:09

Praveen Lobo