У меня есть таблица следующим образом:SQL Update таблицы из другого
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)
Я пытаюсь обновить booking.update_at от statemachine_history.updated_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', вероятно, будет выбирать как идентификатор, так и 'id', а не' id' из statemachine_history. –
@JoachimIsaksson, так как я могу это исправить? – Dahab
@JoachimIsaksson, извините тег, добавленный по ошибке, изменил его :) – Dahab