mySQL 连接同一个表两次
问题描述:
我正在尝试:
select
audit_log_entries.created_at,
audit_log_orig_term_types.name as originator,
audit_log_orig_term_types.name as terminator
from audit_log_entries
join audit_log_orig_term_types on audit_log_entries.originator_type_id = audit_log_orig_term_types.id
join audit_log_orig_term_types on audit_log_entries.terminator_type_id = audit_log_orig_term_types.id;
我认为意图很明确,我想要发起者和终结者的两个名字.我在第一个表中有他们的 ID,在另一个表中有他们的名字.
I think the intent is clear, I want both names for the originator and terminator. I have their IDs in the first table and the names on the other table.
我由此得到一个错误:ERROR 1066 (42000): Not unique table/alias: 'audit_log_orig_term_types'
语法错误在哪里?
答
你可以这样:
select
audit_log_entries.created_at,
audit1.name as originator,
audit2.name as terminator
from audit_log_entries
join audit_log_orig_term_types audit1 on audit_log_entries.originator_type_id = audit1.id
join audit_log_orig_term_types audit2 on audit_log_entries.terminator_type_id = audit2.id;