0

I want to show all the remaining data in the table except some columns. However, despite doing a lot of research, I get stuck in many places. What is the logic of this?

SELECT * ,c.phone AS telefon FROM accounts c;

Table:

user_id  phone    teléfono
    1    null     99999999
sabcan
  • 407
  • 3
  • 15
  • 3
    No, this cannot be done. You are [not the first to wish for that](https://www.postgresql.org/message-id/flat/CANcm6wbR3EG7t-G%3DTxy64Yt8nR6YbpzFRuTewJQ%2BkCq%3DrZ8M2A%40mail.gmail.com), however, and trawling the archives can give you an idea of the reasons why PostgreSQL doesn't have that. – Laurenz Albe Sep 22 '22 at 08:19
  • When using a * you never know what you get. When a column has been added, you get one column extra in your result. When deleted, you don't get it anymore. But the application that is reading the results, is not prepared for these changes. To me a * tells me the query is not ready for production. – Frank Heikens Sep 22 '22 at 09:52

1 Answers1

3

You can't do that. Instead, you specify the columns that you do want.

SELECT column_a, column_b, column_x FROM table;

It is a good idea in query to really only specify the columns that you really need. Otherwise you are in for surprises if the table schema might change in the future.

bugmenot123
  • 1,069
  • 1
  • 18
  • 33
  • Thanks for the help. That came to mind for me too, but it's a long road. There has to be a shorter way to this.. I can do this, I know that. I'm just missing a point. I think I can do this with is not. But I guess it takes some experience. – sabcan Sep 22 '22 at 08:18
  • 1
    Nope. See e. g. https://stackoverflow.com/questions/729197/exclude-a-column-using-select-except-columna-from-tablea or https://dba.stackexchange.com/questions/1957/sql-select-all-columns-except-some – bugmenot123 Sep 22 '22 at 08:25