简体   繁体   English

选择查询以检索表中特定行的主键值

[英]Select query to retrieve the value of primary key for a specific row in a table

I am struggling to retrieve the value of primary key for a table. 我正在努力检索表的主键值。 We are using MS SQL Server 2005. The database was designed years back by somebody else (he didn't follow the normalization rules at all). 我们正在使用MS SQL Server 2005.数据库是由其他人多年设计的(他根本没有遵循规范化规则)。 He used Key (which is a keyword in sql server) as the column name for primary key of a table. 他使用Key(sql server中的关键字)作为表的主键的列名。 So I cannot use query like this: select key from table_name where column2 = ? 所以我不能使用这样的查询: select key from table_name where column2 = ?

Could anyone help to write a query to get the value of the primary key for a specific row something like this: select primary_key from tbale_name where column2 = ? 任何人都可以帮助编写查询来获取特定行的主键值,如下所示: select primary_key from tbale_name where column2 = ?

Yes you can, simply wrap column names in backticks: 是的,你可以,只需在反引号中包装列名:

select `key` from `table_name` where `column2` = ?

Alternatively, depending on your DB, you might use square brackets: 或者,根据您的数据库,您可以使用方括号:

select table_name.[key] from table_name where table_name.[column2] = ?

Edit: I see you said "MS SQL". 编辑:我看到你说“MS SQL”。 I think that one works with the square brackets. 我认为一个方括号。 MySQL accepts the backtick syntax. MySQL接受反引号语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM