mySQL 连接同一个表两次 [英] mySQL join same table twice
本文介绍了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;
这篇关于mySQL 连接同一个表两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文