How can I “select *” from a table in MySQL but omit certain columns?

后端 未结 9 1371
时光取名叫无心
时光取名叫无心 2020-12-03 11:43

I have a table with the following columns:

id,name,age,surname,lastname,catgory,active

Instead of: SELECT name,age,surname,lastname,c

相关标签:
9条回答
  • 2020-12-03 12:08

    I'm fairly certain you can't. Probably the best way I can think of is to create SELECT name, age, surname, lastname, category FROM table as a view, then just SELECT * FROM view. I prefer to always select from a view anyway.

    However, as others have pointed out, if another column gets added to the view your application could fail. On some systems as well (PostgreSQL is a candidate) you cannot alter the table without first dropping the view so it becomes a bit cumbersome.

    0 讨论(0)
  • 2020-12-03 12:10

    Unless there's some special extension in MySql you cannot do that. You either get all, or have to explicitly state what you want. It is best practice to always name columns, as this will not alter the query behaviour even if the underlying table changes.

    0 讨论(0)
  • 2020-12-03 12:10

    You should not be using select * anyway. Enumerate the columns you want and only the columns you want, that is the best practice.

    0 讨论(0)
提交回复
热议问题