MySQL SQL SELECT * FROM tbl(隐藏 1 列)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6426913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL SELECT * FROM tbl (HIDE 1 COLUMN)
提问by eistrati
I'm trying to do SELECT * FROM tbl
, but out of 20 columns I don't want 1 particular column to be selected. Is there a dynamic way to do so? Just hate to specify 19 columns in the query!
我正在尝试这样做SELECT * FROM tbl
,但在 20 列中我不希望选择 1 个特定列。有没有动态的方法来做到这一点?只是讨厌在查询中指定 19 列!
回答by HLGEM
Just specify the columns. You should be doing that all the time anyway as select * is a very poor programming choice. I don't know about mysql but in SQL Server I can drag all the columns over from the object browser and delete the ones I don't want, it takes seconds. Perhaps the interface you use has a similar functionality.
只需指定列。无论如何,您应该一直这样做,因为 select * 是一个非常糟糕的编程选择。我不了解 mysql,但在 SQL Server 中,我可以从对象浏览器中拖动所有列并删除我不想要的列,这需要几秒钟。也许您使用的界面具有类似的功能。
回答by Josh
Try this out. Like he says, the SQL isn't pretty.
试试这个。就像他说的那样,SQL 并不漂亮。
回答by Ed Guiness
Well you (or I) could spend a bit of time writing some dynamic sql to query the columns in a table, generating a sql statement minus the column you want to ignore, then executing that dynamic sql.
好吧,您(或我)可以花一些时间编写一些动态 sql 来查询表中的列,生成一个 sql 语句减去您要忽略的列,然后执行该动态 sql。
Or you could take a deep breath and code it up for the 19 columns.
或者,您可以深呼吸并为 19 列编码。
I know which I'd choose. I know which way is more maintainable and less error prone.
我知道我会选择哪个。我知道哪种方式更易于维护且不易出错。
回答by IAmTimCorey
Interesting question but the direct answer is no, that is not possible. However, you can use a view or a stored procedure that will allow you to write out the 19 columns once and then just call the stored procedure or view using a SELECT * FROM view
or exec storedproc
有趣的问题,但直接的答案是否定的,那是不可能的。但是,您可以使用允许您一次写出 19 列的视图或存储过程,然后只需使用SELECT * FROM view
或调用存储过程或视图exec storedproc
The only issue here is that if you add/remove/renamed a column to your table, you will need to update your object (view or stored procedure) to reflect the new/removed/renamed column.
这里唯一的问题是,如果向表中添加/删除/重命名列,则需要更新对象(视图或存储过程)以反映新/删除/重命名的列。
回答by K6t
insded of star write the required column name In that ,, e.i is
insded 的 star 写出所需的列名 在那个 ,, ei 是
SELECT name,password...etc FROM tbl
回答by Black Light
I would agree with all the answers re. the merits of doing "select * from ..."
我同意所有的答案。做“select * from ...”的优点
However, if you reallywanted to, you could do a "select *" into a temporary table, do an "alter table" to drop the column(s) you don't want, and then do a "select *" from the temporary table.
但是,如果您真的想要,您可以在临时表中执行“select *”,执行“alter table”以删除您不想要的列,然后从临时表。
Still... not very nice because the main point is that, should your table structure change (and get some extra columns), consumers of your selected data would now be getting more than they expect. "Select *" is, in most cases, just lazy coding.
仍然......不是很好,因为要点是,如果您的表结构发生变化(并获得一些额外的列),您所选数据的消费者现在将获得比他们预期的更多。在大多数情况下,“选择 *”只是懒惰的编码。