Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nov 2001 cylauFoxPro Chapter 111 Chapter 11 Joining Tables.

Similar presentations


Presentation on theme: "Nov 2001 cylauFoxPro Chapter 111 Chapter 11 Joining Tables."— Presentation transcript:

1 Nov 2001 cylauFoxPro Chapter 111 Chapter 11 Joining Tables

2 Nov 2001 cylauFoxPro Chapter 112 Union, Intersection and Minus in Set Set A = {1, 2, 3} Set B = {1, 3, 5, 7} Set C = {2, 4} zA Union B = {1, 2, 3, 5, 7} && 1, 3 once zA Intersection B = {1, 3} zA Minus B = {2} zB Minus A = {5, 7} zA Union B Union C = {1, 2, 3, 4, 5, 7}

3 Nov 2001 cylauFoxPro Chapter 113 UNION SELECT * FROM setA; UNION; SELECT * FROM setB zSimilar to APPEND FROM in FoxPro zOriginal files (setA and setB) not affected zrecords present both in setA and setB will appear only once. zsetA and setB must have exactly the same structure

4 Nov 2001 cylauFoxPro Chapter 114 Example zS7(name, club) ChanMusic LeeDrama CheungEnglish WongCYC Ho zS6(name, club) WongCYC KongLocomotive

5 Nov 2001 cylauFoxPro Chapter 115 UNION (2) SELECT * FROM s7; UNION; SELECT * FROM s6 zIf the two name fields are of the same type and width, this will succeed. zIf the name of a merged field is different in the two source files, that of the first file will be used for the output (e.g. name1 <> name2 --> name1)

6 Nov 2001 cylauFoxPro Chapter 116 Result of Union ChanMusic CheungEnglish Ho KongLocomotive LeeDrama WongCYC z“WongCYC” appears once zSorted by name (the 1st field)

7 Nov 2001 cylauFoxPro Chapter 117 UNION (3) SELECT * FROM setA;&& record X, Y, Z UNION; SELECT * FROM setB;&& record W, X UNION; SELECT * FROM setC&& record Z, K zresult : record X, Y, Z, W, K

8 Nov 2001 cylauFoxPro Chapter 118 UNION ALL SELECT * FROM setA; UNION ALL; SELECT * FROM setB; zrecords present both in setA and setB will appear twice. z{1, 2, 3} Union {1, 3, 5} = {1,2,3,1,3,5}

9 Nov 2001 cylauFoxPro Chapter 119 Example SELECT * FROM s6; UNION ALL; SELECT * FROM s7; WongCYC KongLocomotive ChanMusic LeeDrama CheungEnglish WongCYC Ho z “WongCYC” appears twice z Result obtained by placing s6 on top of s7 z NOT Sorted

10 Nov 2001 cylauFoxPro Chapter 1110 JOIN vs UNION zUNION pools records from various tables with same structure together to make new (virtual) table. yAdding rows of two tables of the same structure to form a new one zJOIN combines fields from various tables to make new (virtual) table. yAdding columns of two tables to form a new one

11 Nov 2001 cylauFoxPro Chapter 1111 Cross-join (Natural join) Table1 (2 records, m columns) R1 R2 Table2 (3 records, n columns) r1 r2 r3

12 Nov 2001 cylauFoxPro Chapter 1112 Cross-join (Natural join) (2) SELECT * FROM table1, table2 zwill give (2 x 3 = 6 records, m+n columns) R1r1 R1r2 R1r3 R2r1 R2r2 R2r3

13 Nov 2001 cylauFoxPro Chapter 1113 Example zTEAMA (name, house, skill, sex) ChanFaithbeginner f LeeHopeadvancedm CheungLovebeginnerm WongWisdomadvancedf zTEAMB (name, house, skill, sex) AuFaithadvancedm ChowHopebeginnerf WuLoveadvancedf MaWisdombeginnerm

14 Nov 2001 cylauFoxPro Chapter 1114 Result of Cross-join SELECT * from teama, teamb Name_aName_bOther fields ChanAu... ChanChow ChanWu ChanMa LeeAu LeeChow LeeWu LeeMa CheungAu… WongMa... 4 x 4 = 16 records = 8 columns

15 Nov 2001 cylauFoxPro Chapter 1115 INNER JOIN (EQUI-JOIN) zChoosing partners from the same house SELECT * FROM teama;&& Method 1 INNER JOIN teamb; ON teama.house = teamb.house SELECT FROM teama, teamb;&& Method 2 WHERE teama.house = teamb.house name_ahouse_aname_b(other fields) ChanFaithAu… LeeHopeChow… CheungLoveWu… WongWisdomMa...

16 Nov 2001 cylauFoxPro Chapter 1116 INNER JOIN with ‘AS’ zPairing member with the same skill SELECT a.name, a.skill, b.name FROM teama AS a; INNER JOIN teamb AS b ON a.skill=b.skill name_askillname_b WongadvancedAu LeeadvancedAu CheungbeginnerChow ChanbeginnerChow WongadvancedWu LeeadvancedWu CheungbeginnerMa ChanbeginnerMa

17 Nov 2001 cylauFoxPro Chapter 1117 JOIN and WHERE SELECT a.name, a.house, b.name; FROM teama AS a; INNER JOIN teamb AS b; ON a.house = b.house; WHERE a.house IN (‘Faith’, ‘Wisdom’) zWHERE is checked first before JOIN, though JOIN appears before WHERE name_ahousename_b ChanFaithAu WongWisdomMa

18 Nov 2001 cylauFoxPro Chapter 1118 INTERSECT We don’t have (in FoxPro) : SELECT * FROM setA; INTERSECT; SELECT * FROM setB zUse other SQL commands to simulate the results

19 Nov 2001 cylauFoxPro Chapter 1119 Example: Clubs in 2 Schools zOurClub (clubID, name)&& clubs in our school LFLogic Fun MSMaths and Sci SPSpeech LMLocomotive zOtherClub (clubID, name)&& clubs in other school MSMaths and Sci SPSpeech FBFootball

20 Nov 2001 cylauFoxPro Chapter 1120 INTERSECT (2) SELECT c.clubID, c.name; FROM ourclub AS c; INNER JOIN otherclub AS o; ON c.name = o.name zIf setA and setB have exactly the same structure, this can give their intersection. clubIDname MSMaths and Sci SPSpeech

21 Nov 2001 cylauFoxPro Chapter 1121 INNER JOIN (Non-equi-join) zThe join condition is other than equality. z>,, >=, <= SELECT a.name home, b.name visitor; FROM teama a; INNER JOIN teamb b;&& note: ‘AS’ omitted ON a.skill<> b.skill and a.sex>b.sex homevisitor LeeChow CheungWu

22 Nov 2001 cylauFoxPro Chapter 1122 Joining more than two tables SELECT a.name, b.name, h.colour FROM teama a; INNER JOIN teamb b ON a.house=b.house; INNER JOIN hs_colour h ON a.house=h.house What is the result if TABLE hs_colour is housecolour Faithred Hopeorange Lovegreen Wisdomblue Name_aName_bcolour ChanAured LeeChoworange CheungWugreen WongMablue

23 Nov 2001 cylauFoxPro Chapter 1123 Joining to oneself Table ISA(memberclass) adagirl bobboy girlhuman boyhuman

24 Nov 2001 cylauFoxPro Chapter 1124 Joining to oneself (2) SELECT *; FROM isa AS a; INNER JOIN isa AS b; ON a.member = b.member Member_aclass_amember_bclass_b adagirladagirl bobboybobboy girlhumangirlhuman boyhumanboyhuman

25 Nov 2001 cylauFoxPro Chapter 1125 Joining to oneself (3) SELECT a.member, “ is a “, b.class; FROM isa AS a; INNER JOIN isa AS b; ON a.class = b.member Memberclass‘is a’memberclass adagirladagirl bobboybobboy girlhumangirlhuman boyhumanboyhuman Result: ada is a human bob is a human

26 Nov 2001 cylauFoxPro Chapter 1126 Outer Joins zLike an inner join, outer join is made with a join condition. zHowever, an outer join would extract all records that match plus some which do not match the join condition.

27 Nov 2001 cylauFoxPro Chapter 1127 Example S7(name, club) ChanMusic LeeDrama CheungEnglish WongCYC Ho&& Ho does not join any club CLUBINFO(club, teacher) MusicAu DramaChow EnglishWu CYCMa LocomotiveMak&& No one takes Locomotive

28 Nov 2001 cylauFoxPro Chapter 1128 Left Outer Join SELECT s7.name, s7.club, c.teacher FROM s7; LEFT OUTER JOIN clubinfo as c; ON s7.club = c.club nameclubteacher ChanMusicAu LeeDramaChow CheungEnglishWu WongCYCMa Ho.NULL.&& not matched zALL records on the LEFT are shown (matched or unmatched

29 Nov 2001 cylauFoxPro Chapter 1129 Right Outer Join SELECT s7.name, s7.club, c.teacher FROM s7; RIGHT OUTER JOIN clubinfo as c; ON s7.club = c.club nameclubteacher ChanMusicAu LeeDramaChow CheungEnglishWu WongCYCMa.NULL..NULL.Mak&& not matched zALL records on the RIGHT are shown (matched or unmatched

30 Nov 2001 cylauFoxPro Chapter 1130 Full Outer Join SELECT s7.name, s7.club, c.teacher FROM s7; FULL OUTER JOIN clubinfo as c; ON s7.club = c.club nameclubteacher ChanMusicAu LeeDramaChow CheungEnglishWu WongCYCMa Ho.NULL..NULL..NULL.Mak ALL unmatched records on both sides are shown


Download ppt "Nov 2001 cylauFoxPro Chapter 111 Chapter 11 Joining Tables."

Similar presentations


Ads by Google