gpt4 book ai didi

postgresql - 在选择中使用变量(需要撇号)

转载 作者:行者123 更新时间:2023-11-29 12:56:02 28 4
gpt4 key购买 nike

psql(9.6.1,服务器 9.5.5)

员工

     Column     |            Type             |                            Modifiers                            | Storage  | Stats target | Description 
----------------+-----------------------------+-----------------------------------------------------------------+----------+--------------+---- ---------
employee_id | integer | not null default nextval('employees_employee_id_seq'::regclass) | plain | |
first_name | character varying(20) | | extended | |
last_name | character varying(25) | not null | extended | |
email | character varying(25) | not null | extended | |
phone_number | character varying(20) | | extended | |
hire_date | timestamp without time zone | not null | plain | |
job_id | character varying(10) | not null | extended | |
salary | numeric(8,2) | | main | |
commission_pct | numeric(2,2) | | main | |
manager_id | integer | | plain | |
department_id | integer

为了 self 教育,我想使用一个变量。

这个请求的结果适合我:

hr=> select last_name, char_length(last_name) as Length from employees where substring(last_name from 1 for 1) = 'H' order by last_name;
last_name | length
-----------+--------
Hartstein | 9
Higgins | 7
Hunold | 6
(3 rows)

但为了 self 教育,我想使用一个变量:

\set chosen_letter 'H'
hr=> select last_name, char_length(last_name) as Length from employees where substring(last_name from 1 for 1) = :chosen_letter order by last_name;
ERROR: column "h" does not exist
LINE 1: ...ployees where substring(last_name from 1 for 1) = H order by...
^

那些撇号似乎毁了一切。我无法解决这个问题。

你能帮我理解如何使用变量来获取上面的结果吗?

最佳答案

尝试使用:

\set chosen_letter '''H'''

关于postgresql - 在选择中使用变量(需要撇号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43172738/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com