繁体   English   中英

来自另一个的 SQL 更新表

[英]SQL Update table from another

我有如下表:

dev=> \d statemachine_history
                                     Table "public.statemachine_history"
    Column     |           Type           |                             Modifiers                             
---------------+--------------------------+-------------------------------------------------------------------
 id            | bigint                   | not null default nextval('statemachine_history_id_seq'::regclass)
 schema_name   | character varying        | not null
 event         | character varying        | not null
 identifier    | integer                  | not null
 initial_state | character varying        | not null
 final_state   | character varying        | not null
 triggered_at  | timestamp with time zone | not null default statement_timestamp()
 triggered_by  | text                     | 
 command       | json                     | 
 flag          | json                     | 
 created_at    | timestamp with time zone | 
 created_by    | json                     | 
 updated_at    | timestamp with time zone | 
 updated_by    | json                     | 
Indexes:
    "statemachine_log_pkey" PRIMARY KEY, btree (id)
    "unique_statemachine_log_id" UNIQUE, btree (id)
    "statemachine_history_identifier_idx" btree (identifier)
    "statemachine_history_schema_name_idx" btree (schema_name)

dev=> \d booking             
                                      Table "public.booking"
     Column     |           Type           |                      Modifiers                       
----------------+--------------------------+------------------------------------------------------
 id             | bigint                   | not null default nextval('booking_id_seq'::regclass)
 pin            | character varying        | 
 occurred_at    | timestamp with time zone | 
 membership_id  | bigint                   | 
 appointment_id | bigint                   | 
 created_at     | timestamp with time zone | 
 created_by     | json                     | 
 updated_at     | timestamp with time zone | 
 updated_by     | json                     | 
 customer_id    | bigint                   | 
 state          | character varying        | not null default 'booked'::character varying
Indexes:
    "booking_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "booking_appointment_id_fkey" FOREIGN KEY (appointment_id) REFERENCES appointment(id)
    "booking_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customer(id)
    "booking_membership_id_fkey" FOREIGN KEY (membership_id) REFERENCES membership(id)
Referenced by:
    TABLE "booking_decline_reason" CONSTRAINT "booking_decline_reason_booking_id_fkey" FOREIGN KEY (booking_id) REFERENCES booking(id)

我正在尝试从 statemachine_history.updated_at 更新booking.update_at

让你知道这两个表之间存在一对多的关系,所以我想要 MAX(statemachine_history.updated_at)

我的尝试是:

UPDATE booking SET updated_at=
                    (
                        SELECT MAX(updated_at)
                        FROM statemachine_history
                        WHERE schema_name='Booking'
                        AND identifier=id
                        GROUP BY identifier
                    );

然而,bookings.updated_at 变为空

您真正需要做的就是通过明确命名来确保id引用booking.id

UPDATE booking SET updated_at=
                (
                    SELECT MAX(updated_at)
                    FROM statemachine_history
                    WHERE schema_name='Booking'
                    AND identifier = booking.id
                    GROUP BY identifier
                );

用于测试的快速 SQLfiddle

如果查询有实时要求,您将需要查看 TomH 在另一个答案中的加入。

这应该做你需要的:

UPDATE B
SET
    updated_at = SQ.max_updated_at
FROM
    Booking B
INNER JOIN
(
    SELECT
        identifier,
        MAX(updated_at) AS max_updated_at
    FROM
        Statemachine_History SH
    GROUP BY
        identifier
) AS SQ ON SQ.identifier = B.id

[已解决] PSQL 查询:

UPDATE booking SET updated_at=
(
  SELECT MAX(updated_at)
  FROM statemachine_history
  WHERE schema_name='Booking'
        AND identifier=booking.id
  GROUP BY identifier
) WHERE exists(SELECT id FROM statemachine_history WHERE schema_name='Booking' AND identifier=booking.id);

这部分:

WHERE exists(SELECT id FROM statemachine_history WHERE schema_name='Booking' AND identifier=booking.id);

是为了避免更新booking.updated_at,以防statemachine_history表中没有这种关系

暂无
暂无

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

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