考虑以下表格:
人:
id: integer
name: string
朋友:
id1: integer
id2: integer
我想找到所有朋友,所以我使用这样的东西:
People.joins("JOIN friends ON friend.id1 = people.id JOIN friends ON friend.id2 = people.id")
哪个生成此SQL:
SELECT "people".* FROM "people"
JOIN friends ON friends.id1 = people.id
JOIN people ON friends.id2 = people.id
以下错误:
(pry) output error: #<ActiveRecord::StatementInvalid:
PG::DuplicateAlias: ERROR: table name "people" specified more than once
多次指定people
有什么问题?
答案 0 :(得分:2)
给他们不同的别名:
SELECT p1.* FROM people p1
JOIN friends ON friends.id1 = p1.id
JOIN people p2 ON friends.id2 = p2.id