Home > database >  Three joint query table, seems to be complicated
Three joint query table, seems to be complicated

Time:02-04



A table
Phone number order provinces gonghaowu
111 Beijing 1
112 Shanghai 1
113 3 Beijing 2
114 4 Beijing 3
115 5 Beijing 4
111 Beijing 5

Table B
Phone number order provinces coding order score
113 Beijing 1111 1 10
114, 1112 Shanghai 2
115 Beijing 1113 3 10
116 Beijing 1114 4, 10
117 5 1115 Beijing 10
118 Beijing 1116 2 10

C table
Phone number order province work number coding order time
113 Beijing 1 1111 1 10
114 Shanghai 1 1112 2 9
113 3 Beijing 2 1113 3 8
3 1114 4 114 Beijing 7
115 5 Beijing 4 5 6 1115
111 Beijing 5 1116 2

The results
Phone number order province work number coding order scoring time
111 Beijing 5 1116 2 0
112 Shanghai 1 0 0 0 0
113 3 Beijing 2 1111 1 10 10
4 Beijing 3 1112 114 2 10 9
115 4 5 Beijing 1113 3 10 6
116, 4 0 1114 Beijing 4 0
117 5 0 1115 Beijing 10 0
Beijing 118 2 0 1116, 2 0

Phone number matching, the mobile phone number list in the table may be repeated, subsequent consider to heavy or the same phone number, and according to the heavy computational
Need is the result of all the phone number, the completion of three tables can get all information
Currently made the whole outer joins, but is the field of all three tables have emerged, and mobile phone number no summary to a list

Think of a way to query the first three tables do not duplicate phone number for the new table, then use the new table cell phone number, then to connect the ABC table respectively

There is no other way

CodePudding user response:

Think of the following two requirements logic needs to be addressed

1. Each table if there is a repeated phone number, to heavy or what is the logic of polymerization

2. The table consists of three fields (except for mobile phone number), relevance is not empty, after should take which table,

CodePudding user response:

That there should be A main side tables, such as A is the main table

Select * from a
Left the join b on a. mobile phones=b.
Left the join c on a. mobile phones=c.
  • Related