另一个SQL Update表

时间:2016-03-14 17:43:55

标签: mysql sql postgresql psql

我有以下表格:

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)

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

让您知道2个表之间存在一对多的关系,所以我想要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变为空

3 个答案:

答案 0 :(得分:1)

您真正需要做的就是通过明确命名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 ); ;

splitlines

A quick SQLfiddle to test with

如果查询有实时要求,您可以查看TomH加入另一个答案。

答案 1 :(得分:0)

这应该做你需要的:

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

答案 2 :(得分:0)

[已解决] 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表中没有这样的关系