Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WHERE cast column name equals

Is it possible to write a where statement that uses the column name that has been assigned to the column rather than the column name that actually exists? The entire table is created dynamically with the column names stored in another table. I have a function that allows users to add filters to an jobect before retrieving the set of data that goes with it.

Example of what I am trying:

SELECT id, 
  col1 as [description], 
  col2 as [date1], 
  col3 as [image], 
  col4 as [date2], 
  col5 as [link], 
  col6 as [location], 
  col7 as [price], 
  col8 as [title]
FROM tableName
WHERE [title] = 'Lemons'
like image 317
Rumpleteaser Avatar asked Dec 13 '25 00:12

Rumpleteaser


1 Answers

Make your original query a subquery:

SELECT *
FROM (
   SELECT id, 
     col1 as [description], 
     col2 as [date1], 
     col3 as [image], 
     col4 as [date2], 
     col5 as [link], 
     col6 as [location], 
     col7 as [price], 
     col8 as [title]
   FROM tableName
) as subquery
WHERE [title] = 'Lemons'
like image 160
Keith Avatar answered Dec 14 '25 17:12

Keith