MySQL:如何按数字选择列? [英] MySQL: How to select a column by number?
问题描述
我正在编写程序,我需要按编号而不是按名称SELECT
列名.
I'm writing a program, and I have the need to SELECT
column names by number, rather than by name.
也就是说,表的属性为:
That is, say the attributes to my table are:
SSN, FirstName, LastName, MiddleName, Address, City, State, Zip
我如何仅从列0(SSN),1(名字),2(姓氏),6(状态)中选择数据
How could I select the data just from columns 0 (SSN), 1 (FirstName), 2(LastName), 6 (State)
是否有一种无需深入研究information schema
的方法?
Is there a way to do this without delving into information schema
?
推荐答案
您应该重新考虑您的设计.一个表可以改变,它可以演化,列顺序非常善变.您不应该对列号进行硬编码.
You should reconsider your design. A table can be changed, it can evolve, column order is very fickle. You should NOT hardcode column numbers.
附录
通过进化"和变幻无常",我并不是说数据库引擎可以移动它们.那几乎可以解决.不,我的意思是将来需求可能会改变,并且表设计可以更新.如果您列出实际的列名而不是列位置顺序,将来改编旧代码将变得更加容易.想想列位置就像行位置一样:不要期望结果基于输入顺序有特定的顺序,而是 make 您的查询会为您提供所需的顺序.
By "evolve" and "fickle", I don't mean that the database engine can move them around. That's pretty much fix. No, I mean that needs can change in the future and the table design can be updated. It will be easier in the future to adapt your old code if you list the actual column names rather than the column position order. Think of column position just like row position: don't expect a particular order in the result based on input order, but rather make your query give you the order you need.
这篇关于MySQL:如何按数字选择列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!