简体   繁体   English

选择 PostgreSQL 中的数字命名列返回?列?

[英]Selecting numeric named columns in PostgreSQL returns ?column?

I am using PostgreSQL 13.7 (Debian 13.7-0+deb11u1) and have the following table:我正在使用 PostgreSQL 13.7 (Debian 13.7-0+deb11u1) 并有下表:

CREATE TABLE public.meterdata (
  "0.0.0" numeric(8,0) NOT NULL,
  "0.9.1" numeric(7,0) NOT NULL,
  "0.9.2" numeric(7,0) NOT NULL,
  "1.2.0" real,
  "1.6.0" real NOT NULL,
  "1.8.0" real NOT NULL,
  "1.8.1" real,
  "1.8.2" real,
  "3.8.0" real NOT NULL,
  "3.8.1" real,
  "3.8.2" real,
  "F.F" numeric(8,0) NOT NULL,
  factor numeric(4,0) NOT NULL
);

Now when I try to get some data:现在,当我尝试获取一些数据时:

SELECT '0.0.0','1.8.0',factor FROM meterdata

The returned data from column 'factor' is right, but the column with the numeric names appears as name ?column?从列“因子”返回的数据是正确的,但是带有数字名称的列显示为 name ?column? and the value of each row in this column is the column name (like 1.8.0 ).此列中每一行的值是列名(如1.8.0 )。

I tried this with PHP, phppgadmin and even psql from the command line.我在命令行中使用 PHP、phppgadmin 甚至psql进行了尝试。 All return ?column?全部返回?column? for each column starting with a number.对于以数字开头的每一列。

I know it is not optimal to use numbers as column names, but is it somehow possible to use this way or am I forced to use letters as column names?我知道使用数字作为列名并不是最佳选择,但是是否可以以某种方式使用这种方式,或者我是否被迫使用字母作为列名? Or is that some bug I am running into?还是我遇到的一些错误? Thanks in advance for explaining!提前感谢您的解释!

In PostgreSQL, if you use non-standard column names you must enclose them in double-quotes.在 PostgreSQL 中,如果使用非标准列名,则必须将它们括在双引号中。 There is no issue with you using column names that start with a number or having a period, but you need to consistently double-quote them.使用以数字开头或带有句点的列名没有问题,但您需要始终用双引号括起来。

Single-quotes indicate literal strings, so in your query the query processor sees a constant string, not a column name.单引号表示文字字符串,因此在您的查询中,查询处理器会看到一个常量字符串,而不是列名。 In cases where the column name is not clear (could also be for computed columns, for instance), PostgreSQL will print ?column?如果列名不明确(例如,也可能是计算列),PostgreSQL 将打印?column? as a placeholder.作为占位符。

SELECT "0.0.0", "1.8.0", factor FROM meterdata

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

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