Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select all except for a specific column

Tags:

postgresql

I have a table with more than 20 columns, I want to get all columns except for one which I'll use in a conditional expression.

SELECT s.* (BUT NOT column1), 
   CASE WHEN column1 is null THEN 1 ELSE 2 END AS column1 
from tb_sample s;

Can I achieve it in PostgreSQL given the logic above?

like image 330
cjslv Avatar asked Aug 09 '26 02:08

cjslv


1 Answers

It may not be ideal, but you can use information_schema to get the columns and use the column to exclude in the where clause.

That gives you a list of all the column names you DO want, which you can copy/paste into your select query:

select textcat(column_name, ',')
from information_schema.columns
where table_name ='table_name' and column_name !='column_to_exclude';
like image 129
Ryan Wittrup Avatar answered Aug 10 '26 23:08

Ryan Wittrup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!