1

我有一个名为concept-relation包含 3 列的表

(relationID, firstConceptID, secondConceptID)

我有一个名为concept包含 2 列的表

(ID, name)

我想获得的名称firstConceptIDsecondConceptID时间relationID = 22

这是我提出的查询。

select * from (
      select name as source from concept where concept.ID in (
      select firstConceptID from `concept-relation` where relationID = 22
       ) 
) as e,
(
      select name as des from concept where concept.ID in (
      select secondConceptID from `concept-relation` where relationID = 22
     )
)as e

它运作良好,但我想知道执行此类查询的最佳做法是什么?

4

1 回答 1

4

需要自连接以使其更清洁通常被认为是最佳实践,因为它避免了子选择/“IN”

SELECT C1.name, C2.name
FROM Concept C1
INNER JOIN concept_Relation CR 
  ON CR.FirstConceptID = C1.ID
INNER JOIN Concept C2 
  ON CR.SecondConceptID = C2.ID
WHERE CR.RelationID = 22
于 2012-05-19T19:29:16.200 回答