开发者

MYSQL: How to refer to the 1st column

开发者 https://www.devze.com 2023-01-17 00:15 出处:网络
SELECT first_name FROM user WHERE FIRST_COLUMN = \'10\' What I need to know is how to reference \"FIRST_COLUMN\" in MYSQL syntax. Th开发者_如何学JAVAe first column can be any name and so I need to m
SELECT first_name FROM user WHERE FIRST_COLUMN = '10'

What I need to know is how to reference "FIRST_COLUMN" in MYSQL syntax. Th开发者_如何学JAVAe first column can be any name and so I need to make it flexible as long it should get the 1st column of any table. thanks


The short answer is : you can't. (in a portable way, there are 'tricks' to look up the name of the first column and similar workarounds)

Many projects use as convention to name the first column ID and use that as the primary key.

By the query it looks like you are uing it as a primary key.

I recommend reading an introduction to relational databases as this is a rather strange request in the context of a relational database.


The relational model does not care one tiny little bit what order columns are in within a table, nor (without an ordering clause) what order rows are returned.

Your requirement makes little sense. What if the first column were a varchar or a date?

The whole point of having named columns is that you reference them by name.

Now DBMS' often contain metadata in system tables, like DB2's sysibm.systables and sysibm.syscolumns, but you need to extract not just the names but all the other metadata as well (column type, size, nullable, and so on) in order to use them properly. We'd probably understand better what you were after if you told us the reason behind doing this.


 SELECT COLUMN_NAME FROM information_schema.COLUMNS 
    WHERE TABLE_NAME = 'tablename' 
    AND ORDINAL_POSITION =1
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号