In depth analysis of sql multi table differentiated joint queries

  • 2020-05-17 06:44:58
  • OfStack

This chapter briefly describes the multi-table query problem I encountered in the example development
Which is mainly about
select A.*,B.*,C.* from A,B,C
and
select A.*,B.*,C.* from A inner join id=B inner join C on A code=C code
The difference between

I recently encountered a project requirement that required a page collection to display information about three associated tables.
Since there are many fields in the actual project, it is simplified as follows:
table A contains the fields UserName, CardCode, ItamCode
table B contains the fields CardCode, CardName
table C contains the fields ItamCode, ItamName

Where the interface appears simultaneously in a data set UserName, CardName, ItamName
In other words, although I have learned the problems of SQL inline and external connection, the previous projects basically involved the implementation of the association between two tables. Now it is the first time to use the implementation of the association between more than two tables. Baidu to find 1 under the data, only to find the original basic principle is a kind of. Ha, I only joined the company last year, so I don't have much experience. Please don't laugh at me!
Er, if the reader is not familiar with SQL basic inlining, external knowledge, please consciously find baidu to go to the wall, the author here do not go through. To make a long story short, let's get down to business...

Discover that there are two ways to implement the above functionality, namely
select A.UserName,B.CardName,C.ItamName from A,B,C
and
select A UserName, B CardName, C. ItamName from table A inner join table B on A. CardCode = B. CardCode inner join table C on A. ItamCode = C. ItamCode
(if the fields are differentiated, skip the table name.)

As far as the above grammar is concerned, the effect of the two implementation methods is the same, but if you really want to compare the pros and cons, or the second implementation method is more practical.
With inner join (full outreach), not only can you freely convert left join (left outreach) and right join (right outreach) according to the actual needs, but also indicate the order of integration, especially the convenience of supporting the optimization of SQL text.

The other one is that when I have a field that can be null, the first query method doesn't get a complete value. For example, in my example above, when table C ItanName is not required, the desired effect can only be achieved through the left outer join (left join).
Those of you who are interested can do 1 on your own and verify the difference between the two methods.


Related articles: