Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 通过一些字符串操作,使用另一个表中的数据更新一个表中的数据_Sql_Postgresql_Concat - Fatal编程技术网

Sql 通过一些字符串操作,使用另一个表中的数据更新一个表中的数据

Sql 通过一些字符串操作,使用另一个表中的数据更新一个表中的数据,sql,postgresql,concat,Sql,Postgresql,Concat,我有两个数据库表,如下所示: content_definition ( content_definition_id bigint , content_title character varying(100) NOT NULL, content_desc character varying(500), content_kwd character varying(300), content_view_url character varying(100),

我有两个数据库表,如下所示:

content_definition
(
    content_definition_id bigint ,
    content_title character varying(100) NOT NULL,
    content_desc character varying(500),
    content_kwd character varying(300),
    content_view_url character varying(100),
    content_edit_url character varying(300),
    content_media_url character varying(300),
    content_type character varying(50),
    site_id integer
)

bl_address
(
    address_id bigserial NOT NULL,
    entity character varying(30),
    entity_id integer,
    address1 character varying(150),
    address2 character varying(50),
    city character varying(50),
    state character varying(50),
    zip character varying(15),
    country character varying(50),
)
我正试图创建一条SQL语句,通过在第一个表中的
content\u view\u url
列中添加第二个表中的
state
来更新该列。第一个表中的
内容id
应等于第二个表中的
实体id
。此外,我需要在所附加的两个字符串之间包含
-
,还需要将state列中的所有空格替换为
-
,并将其转换为小写,因为数据是URL

我试图使用
concat
函数创建一个示例查询,但它对我不起作用:

update content_definition 
set content_view_url = CONCAT(select content_view_url 
                              from content_definition, '-test') 
where content_definition_id = 770764;
我得到一个错误:

错误:“选择”处或附近出现语法错误
SQL状态:42601
字符:64


有什么办法可以做到这一点吗?

在Postgres中,您可以在
更新中使用
FROM
子句。根据您问题中的查询:

update content_definition cd
    set content_view_url = cd2.content_view_url || '-test'
    from content_definition cd2
    where cd2.content_definition_id = 770764;
update content_definition cd
    set content_view_url = content_view_url || a.state
    from bl_address a
    where cd.content_id = a.entity_id;
根据问题:

update content_definition cd
    set content_view_url = cd2.content_view_url || '-test'
    from content_definition cd2
    where cd2.content_definition_id = 770764;
update content_definition cd
    set content_view_url = content_view_url || a.state
    from bl_address a
    where cd.content_id = a.entity_id;
或者根据您的评论:

    set content_view_url = content_view_url || '-' || replace(a.state, ' ', '-')

多亏了这些建议,我一直在寻找的问题是:

update content_definition cd
    set content_view_url = content_view_url || '-' || lower(replace(a.state, ' ' , '-'))
    from bl_address a
    where cd.content_id = a.entity_id and cd.content_type = 'business.location' and cd.site_id = 35;

“它对我不起作用”请描述它是如何“不起作用”的。您的查询与您的问题无关。是否有一种方法可以执行字符串操作,例如在附加的数据之间用-替换空格并添加一个-。@GeoThomas:是的,
=
右侧的所有内容都应该是字符串表达式,您可以使用字符串{对象、运算符、函数}的任何有效组合来构造it@joop,我已经从bl_地址a创建了一个查询,其中cd.content_id=a.entity_id和cd.content_type='bl'和cd.site_id=35;与上述评论的hep一致。我无法理解如何包含替换空格的内容-在添加数据之前,我已经理解了,请更新content_definition cd set content_view_url=content_view_url | |'-| | | lower(替换(a.state,,,-))来自bl_地址a,其中cd.content_id=a.entity_id和cd.content_type='business.location'和cd.site_id=35;这是我想要的查询