~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to docs/join.rst

fixed build warnings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
Join
2
 
====
 
 
b'\\ No newline at end of file'
 
2
====
 
3
 
 
4
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.
 
5
 
 
6
Queries can access multiple tables at once, or access the same table in such a way that multiple rows of the table are being processed at the same time. For instance, in order to list all the 'interest' records together with the location of the associated city. To do this, it's necessary to compare the city column of each row of the 'Interests" table with the name column of all rows in the cities table, and select the pairs of rows where these values match. As such, a JOIN statement involves combining records from two tables by using values common to each. 
 
7
 
 
8
Take the "Interests" table:
 
9
 
 
10
+---------+--------------+--------------+-------------+----------+
 
11
|Id       |DateAdded     |InterestType  |Name         | userID   |
 
12
+=========+==============+==============+=============+==========+
 
13
| 1       |2011-01-02    | Sport        |45           |2         |
 
14
+---------+--------------+--------------+-------------+----------+
 
15
| 2       |2011-01-02    | Art          |10           |4         |
 
16
+---------+--------------+--------------+-------------+----------+
 
17
| 3       |2011-01-02    | Music        |25           |1         |
 
18
+---------+--------------+--------------+-------------+----------+
 
19
| 4       |2011-01-02    | Food         |125          |1         |
 
20
+---------+--------------+--------------+-------------+----------+
 
21
| 5       |2011-01-03    | Music        |40           |2         |
 
22
+---------+--------------+--------------+-------------+----------+
 
23
| 6       |2011-01-03    | Food         |20           |3         |
 
24
+---------+--------------+--------------+-------------+----------+
 
25
 
 
26
 
 
27
Note that the "Id" column is the primary key in the "Persons" table. This means that no two rows can have the same "Id"; it distinguishes two interests even if they have the same name or userID.
 
28
 
 
29
Next, we have the "Persons" table:
 
30
 
 
31
+---------+------------+----------+----------+--------+
 
32
|userId   |LastName    |FirstName |Address   |  City  |
 
33
+=========+============+==========+==========+========+
 
34
| 1       | Larson     | Sue      |3 Cherry  | Chicago|
 
35
+---------+------------+----------+----------+--------+
 
36
| 2       | Roberts    | Teri     |21 Brown  | Chicago|
 
37
+---------+------------+----------+----------+--------+
 
38
| 3       | Peterson   | Kari     |30 Mell   | Reno   |
 
39
+---------+------------+----------+----------+--------+
 
40
| 4       | Anderson   | Kyle     |435 Tyler | Dayton |
 
41
+---------+------------+----------+----------+--------+
 
42
 
 
43
The "userID" column is the primary key in the "Persons" table; in the "Persons" table, it can be used to identify users without using their names. Therefore, the relationship between the two tables above is the "userId" column.
 
44
 
 
45
**Different kinds of SQL JOINs**
 
46
 
 
47
Here are the types of JOIN you can use, and the differences between them. Click for detailed syntax and examples:
 
48
 
 
49
    * JOIN: Return rows when there is at least one match in both tables
 
50
    * LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
 
51
    * RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
 
52
    * FULL JOIN: Return rows when there is a match in one of the tables