Best practices for the order of joined columns in a sql join?

后端 未结 5 552
囚心锁ツ
囚心锁ツ 2020-12-18 00:49

There\'s a big discussion going on at my office on the order of the joined columns in a sql join. I\'m finding it hard explaining it so I\'ll just present the two sql statem

5条回答
  •  清酒与你
    2020-12-18 01:19

    SQL was designed to be readable as usual English text. In a usual text, when we want to specify any mentioned object, we say something like "An object. This object is like that object, this object is related to that object, and this object has those properties." We do not say "That object is like this object, to that object related this object and those properties have this object".

    So, when we adding records of TableB to query from TableA, we need to specify what must have a record from TableB to satisfy current record from TableA. So we must say, wee need that records from TableB, which have an id, equal to TableA.ID. FROM TableA a JOIN TableB b ON b.col = a.col. We must write exactly in this order, to provide easy reading and understanding of this relation.

    And I'm very very sad, that almost all SQL documentation does not provide any kind of reasoning to go one or another direction in this question, but have a lot of examples of wrong writing equation arguments in order of presenting of tables in a query.

提交回复
热议问题