2010 01MySQLJoins
2010 01MySQLJoins
Sheeri K. Cabral
Topics Covered
JOINs
OUTER
INNER
CROSS
Subqueries
DEPENDENT SUBQUERY
DERIVED TABLE
Example
Sample data
work table
Sample data
mysql> SELECT * FROM work;
+---------+---------+------------+--------------+
| work_id | wname
| given
| pct_of_grade |
+---------+---------+------------+--------------+
|
1 | hw1
| 2010-01-01 |
6 |
|
2 | test1
| 2010-01-04 |
6 |
|
3 | hw2
| 2010-01-08 |
6 |
|
4 | test2
| 2010-01-11 |
6 |
|
5 | hw3
| 2010-01-15 |
6 |
|
6 | test3
| 2010-01-18 |
6 |
|
7 | midterm | 2010-01-20 |
15 |
|
8 | hw4
| 2010-01-22 |
6 |
|
9 | test4
| 2010-01-25 |
6 |
|
10 | hw5
| 2010-01-29 |
6 |
|
11 | test5
| 2010-02-01 |
6 |
|
12 | hw6
| 2010-02-05 |
6 |
|
13 | test6
| 2010-02-08 |
6 |
|
14 | final
| 2010-02-12 |
25 |
+---------+---------+------------+--------------+
Sample data
student table
Entries
+------------+-----------------+------------------+
| student_id | name
| email
|
+------------+-----------------+------------------+
|
1 | Sheeri Cabral
| [email protected]
|
|
2 | Giuseppe Maxia | [email protected] |
|
3 | Colin Charles
| [email protected]
|
|
4 | Ronald Bradford | [email protected]
|
+------------+-----------------+------------------+
Sample data
student_work table
Sample data
student_work entries
Sample data
student_work entries
Sample data
Venn Diagram
table 1
overlap
table 2
t1.col1
t1.col2=
t2.col2
t1.col3
t2.col1
t2.col3
MySQL JOINs
USING (col1,col2,...)
CROSS JOIN
Cartesian product
All combinations
SELECT name, wname
FROM student CROSS JOIN work;
INNER JOIN
table 1
overlap
table 2
t1.col1
t1.col2=
t2.col2
t1.col3
t2.col1
t2.col3
INNER JOIN
JOIN clause
ON () or USING(...)
Same results
My Best Practices
grade_num_letter
AS s
AS g
student_id
grade_num_letter
work_id
s.grade_num=
grade_num_letter
g.grade_num
for_grade
OUTER JOIN
OUTER JOIN
OUTER is redundant
table 1
overlap
table 2
t1.col1
t1.col2=
t2.col2
t1.col3
t2.col1
t2.col3
overlap
table 2
t1.col1
t1.col2=
t2.col2
t1.col3
t2.col1
t2.col3
Can be simulated
table 1 overlap
t1.col2
t1.col1=
t1.col3
t2.col1
table 2
t2.col2
t2.col3
table 1 overlap
t1.col2
t1.col1=
t1.col3
t2.col1
table 2
t2.col2
t2.col3
SELECT
FROM tbl1 LEFT JOIN tbl2 ...
WHERE tbl2.col1 IS NULL
UNION ALL SELECT FROM tbl1 RIGHT JOIN tbl2
WHERE tbl1.col IS NULL
NATURAL modifier
Works for:
NATURAL JOIN
Instead of:
SELECT name,grade_num
FROM student INNER JOIN student_work USING
(student_id)
WHERE name='Sheeri Cabral';
Write:
SELECT name,grade_num
FROM student NATURAL JOIN student_work
WHERE name='Sheeri Cabral';
Subqueries
Procedural Thinking
FROM student_work
But.....
That falls apart for test3, because Sheeri did not
take test3.
So now what?
Start with:
Temporary table
CREATE TEMPORARY TABLE grade_to_drop (
student_id tinyint unsigned not null,
work_id tinyint unsigned default null,
grade_num tinyint unsigned default null
);
Temporary table
INSERT INTO grade_to_drop (student_id,
grade_num)
SELECT student_id,min(coalesce(grade_num,0))
FROM student CROSS JOIN work
LEFT JOIN student_work USING
(student_id,work_id)
WHERE wname like 'test_'GROUP BY student_id;
Temporary table
UPDATE grade_to_drop AS gtd INNER JOIN
student_work AS sw USING
(student_id,grade_num)
SET gtd.work_id = sw.work_id ;
UPDATE student_work AS sw INNER JOIN
grade_to_drop AS gtd USING
(student_id,work_id)
SET sw.for_grade='n' ;
Temporary table
Repeat for dropping the lowest homework
DROP TABLE IF EXISTS grade_to_drop;
That's it!
Questions?
Comments?
01/29/10
Presented by:
Sheeri K. Cabral
01/29/10
Topics Covered
JOINs
OUTER
INNER
CROSS
Subqueries
DEPENDENT SUBQUERY
DERIVED TABLE
Example
Sample data
work table
Sample data
mysql> SELECT * FROM work;
+---------+---------+------------+--------------+
| work_id | wname
| given
| pct_of_grade |
+---------+---------+------------+--------------+
|
1 | hw1
| 2010-01-01 |
6 |
|
2 | test1
| 2010-01-04 |
6 |
|
3 | hw2
| 2010-01-08 |
6 |
|
4 | test2
| 2010-01-11 |
6 |
|
5 | hw3
| 2010-01-15 |
6 |
|
6 | test3
| 2010-01-18 |
6 |
|
7 | midterm | 2010-01-20 |
15 |
|
8 | hw4
| 2010-01-22 |
6 |
|
9 | test4
| 2010-01-25 |
6 |
|
10 | hw5
| 2010-01-29 |
6 |
|
11 | test5
| 2010-02-01 |
6 |
|
12 | hw6
| 2010-02-05 |
6 |
|
13 | test6
| 2010-02-08 |
6 |
|
14 | final
| 2010-02-12 |
25 |
+---------+---------+------------+--------------+
5
Sample data
student table
Entries
+------------+-----------------+------------------+
| student_id | name
| email
|
+------------+-----------------+------------------+
|
1 | Sheeri Cabral
| [email protected]
|
|
2 | Giuseppe Maxia | [email protected] |
|
3 | Colin Charles
| [email protected]
|
|
4 | Ronald Bradford | [email protected]
|
+------------+-----------------+------------------+
6
Sample data
student_work table
Sample data
student_work entries
Sample data
student_work entries
Sample data
10
Venn Diagram
table 1
overlap
table 2
t1.col1
t1.col2=
t2.col2
t1.col3
t2.col1
t2.col3
11
MySQL JOINs
USING (col1,col2,...)
CROSS JOIN
Cartesian product
All combinations
SELECT name, wname
FROM student CROSS JOIN work;
13
INNER JOIN
table 1
overlap
table 2
t1.col1
t1.col2=
t2.col2
t1.col3
t2.col1
t2.col3
14
INNER JOIN
15
17
JOIN clause
ON () or USING(...)
Same results
18
My Best Practices
19
grade_num_letter
AS s
AS g
student_id
grade_num_letter
work_id
s.grade_num=
grade_num_letter
g.grade_num
for_grade
20
OUTER JOIN
21
OUTER JOIN
OUTER is redundant
22
table 1
overlap
table 2
t1.col1
t1.col2=
t2.col2
t1.col3
t2.col1
t2.col3
23
overlap
table 2
t1.col1
t1.col2=
t2.col2
t1.col3
t2.col1
t2.col3
Can be simulated
25
table 1 overlap
t1.col2
t1.col1=
t1.col3
t2.col1
table 2
t2.col2
t2.col3
27
table 1 overlap
t1.col2
t1.col1=
t1.col3
t2.col1
table 2
t2.col2
t2.col3
SELECT
FROM tbl1 LEFT JOIN tbl2 ...
WHERE tbl2.col1 IS NULL
UNION ALL SELECT FROM tbl1 RIGHT JOIN tbl2
WHERE tbl1.col IS NULL
28
NATURAL modifier
Works for:
NATURAL JOIN
29
Instead of:
SELECT name,grade_num
FROM student INNER JOIN student_work USING
(student_id)
WHERE name='Sheeri Cabral';
Write:
SELECT name,grade_num
FROM student NATURAL JOIN student_work
WHERE name='Sheeri Cabral';
30
Subqueries
32
Procedural Thinking
33
34
FROM student_work
35
36
37
38
39
40
41
42
But.....
That falls apart for test3, because Sheeri did not
take test3.
So now what?
43
Start with:
44
45
46
48
49
55
Temporary table
CREATE TEMPORARY TABLE grade_to_drop (
student_id tinyint unsigned not null,
work_id tinyint unsigned default null,
grade_num tinyint unsigned default null
);
56
Temporary table
INSERT INTO grade_to_drop (student_id,
grade_num)
SELECT student_id,min(coalesce(grade_num,0))
FROM student CROSS JOIN work
LEFT JOIN student_work USING
(student_id,work_id)
WHERE wname like 'test_'GROUP BY student_id;
57
Temporary table
UPDATE grade_to_drop AS gtd INNER JOIN
student_work AS sw USING
(student_id,grade_num)
SET gtd.work_id = sw.work_id ;
UPDATE student_work AS sw INNER JOIN
grade_to_drop AS gtd USING
(student_id,work_id)
SET sw.for_grade='n' ;
58
Temporary table
Repeat for dropping the lowest homework
DROP TABLE IF EXISTS grade_to_drop;
59
60
That's it!
Questions?
Comments?
61