TAILIEUCHUNG - Hướng dẫn học Microsoft SQL Server 2008 part 28

Bởi vì kéo nhau gia nhập dữ liệu từ hai bộ dữ liệu, nó làm cho cảm giác rằng SQL cần phải biết làm thế nào để phù hợp lên hàng từ những bộ. SQL Server kết hợp các hàng bằng cách kết hợp một giá trị chung cho cả hai bảng. Thông thường, một khóa chính có giá trị từ một bảng đang được xuất hiện với một giá trị khóa ngoại từ bảng thứ cấp. Bất cứ khi nào một hàng từ bảng đầu tiên của trận đấu liên tiếp từ bảng thứ hai, hai hàng được sáp. | Part II Manipulating Data With Select FIGURE 10-4 Building an inner join within Management Studio s Query Designer Because joins pull together data from two data sets it makes sense that SQL needs to know how to match up rows from those sets. SQL Server merges the rows by matching a value common to both tables. Typically a primary key value from one table is being matched with a foreign key value from the secondary table. Whenever a row from the first table matches a row from the second table the two rows are merged into a new row containing data from both tables. The following code sample joins the Tour secondary and BaseCamp primary tables from the Cape Hatteras Adventures sample database. The ON clause specifies the common data USE CHA2 SELECT FROM INNER JOIN ON 232 Merging Data with Joins and Unions 10 The query begins with the Tour table. For every Tour row SQL Server will attempt to identify matching BaseCamp rows by comparing the BasecampID columns in both tables. The Tour table rows and BaseCamp table rows that match will be merged into a new result Tour. TourName Tour. BaseCampID Basecamp. BaseCampID Basecamp. BaseCampName Appalachian Trail 1 1 Ashville NC Outer Banks Lighthouses 2 2 Cape Hatteras Bahamas Dive 3 3 Freeport Amazon Trek 4 4 Ft Lauderdale Gauley River Rafting 5 5 West Virginia Number of rows returned In the preceding query every row in both the Tour and BaseCamp tables had a match. No rows were excluded from the join. However in real life this is seldom the case. Depending upon the number of matching rows from each data source and the type of join it s possible to decrease or increase the final number of rows in the result set. To see how joins can alter the number of rows returned look at the Contact and Order tables of the OBXKites database. The initial row count of contacts is 21 yet when the customers

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.