简体   繁体   English

来自另一个的 SQL 更新表

[英]SQL Update table from another

I have table as following:我有如下表:

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)

AND

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)

I am trying to update the booking.update_at from the statemachine_history.updated_at我正在尝试从 statemachine_history.updated_at 更新booking.update_at

Letting you know that there is a one to many relationship between the 2 tables so i want to MAX(statemachine_history.updated_at)让你知道这两个表之间存在一对多的关系,所以我想要 MAX(statemachine_history.updated_at)

My try is:我的尝试是:

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

However the bookings.updated_at becomes null然而,bookings.updated_at 变为空

All you really need to do is to make sure id reference booking.id by naming it explicitly;您真正需要做的就是通过明确命名来确保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
                );

A quick SQLfiddle to test with .用于测试的快速 SQLfiddle

If there are real time requirements for the query, you'll want to look into TomH's join in another answer though.如果查询有实时要求,您将需要查看 TomH 在另一个答案中的加入。

This should do what you need:这应该做你需要的:

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

[Solved] PSQL Query: [已解决] 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);

This part:这部分:

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

is to avoid updating booking.updated_at, in case there is not such relation in statemachine_history table是为了避免更新booking.updated_at,以防statemachine_history表中没有这种关系

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

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