Chapter 7 - Pursue
Portfolio Database Table Data
item_id | category_id | title | date_created | description | date_posted |
---|---|---|---|---|---|
1 | 1 | a painting | 2001-06-12 | my first painting! | 2013-09-20 16:28:13 |
2 | 3 | another painting | 2007-05-00 | my favorite painting | 2013-09-20 18:15:51 |
3 | 1 | third painting | 2010-07-00 | a commisioned painting | 2013-09-22 20:15:14 |
category_id | name | description | parent_id |
---|---|---|---|
1 | paintings | general painting category | 0 |
2 | drawings | sketches on various media | 0 |
3 | charcoal | drawings using charcoal | 2 |
4 | acrylic | paintings using acrylic | 1 |
5 | oil | traditional paintings using oils | 1 |
tag_id | name | description |
---|---|---|
1 | figurative | images inspired by the human figure |
2 | abstract | colorful abstractions |
item_id | tag_id |
---|---|
1 | 1 |
2 | 1 |
1 | 2 |
3 | 2 |
album_id | item_id | title | description | date_created |
---|---|---|---|---|
1 | 1 | a blank title for album 1 | painting in progress | 2013-09-27 13:45:02 |
2 | 1 | a blank title for album 2 | completed painting | 2013-09-27 12:22:50 |
3 | 2 | another album | 2013-09-27 12:45:32 |
album_id | image_id | sort |
---|---|---|
1 | 1 | 1 |
2 | 1 | 2 |
2 | 2 | 1 |
2 | 3 | 3 |
3 | 4 | 1 |
image_id | title | alt | filename | filepath | height | width | date_uploaded |
---|---|---|---|---|---|---|---|
1 | image | test.png | 320 | 240 | 0000-00-00 00:00:00 | ||
2 | title | testimg2.png | 320 | 240 | 2013-09-22 10:06:42 | ||
3 | title2 | alt test | testimg3.png | images/ | 640 | 480 | 2013-09-30 20:03:52 |
4 | another image | alt2 | testimg4.png | images/ | 800 | 600 | 2013-09-30 20:03:52 |
Advanced Query Examples
-
-
Get all images in all albums associated with the portfolio item with id=1
SELECT DISTINCT(images.filename), images.title FROM portfolio_items LEFT JOIN albums ON albums.item_id = portfolio_items.item_id LEFT JOIN album_images ON album_images.album_id=albums.album_id INNER JOIN images ON album_images.image_id = images.image_id WHERE portfolio_items.item_id=1;
Results filename title test.png image testimg2.png title testimg3.png title2 -
Get the number of albums associated with portfolio item 1
SELECT COUNT(albums.item_id) as `Number of albums` FROM albums INNER JOIN portfolio_items USING (item_id) WHERE portfolio_items.item_id=1;
Results Number of albums 2 -
Get all portfolio item titles in the category 'paintings'
SELECT title FROM portfolio_items INNER JOIN categories AS C USING (category_id) WHERE C.name='paintings';
Results title a painting third painting -
Use a self join to retrieve all the subcategories of the painting category.
SELECT C2.parent_id, C2.name AS 'Subcategory', C1.name AS 'Main Category' FROM categories as C1, categories as C2 WHERE C1.category_id=C2.parent_id AND C1.name='paintings';
Results parent_id Subcategory Main Category 1 acrylic paintings 1 oil paintings -
Use a left join to retrieve all the tags of portfolio item 1.
SELECT tags.name AS 'Tag', tags.description FROM portfolio_items LEFT JOIN item_tags USING(item_id) LEFT JOIN tags USING(tag_id) WHERE portfolio_items.item_id=1;
Results Tag description figurative images inspired by the human figure abstract colorful abstractions -
Use a left join to retrieve all the images of portfolio items tagged with 'figurative'
SELECT images.*, albums.* , portfolio_items.title FROM tags LEFT JOIN item_tags USING(tag_id) LEFT JOIN portfolio_items USING(item_id) LEFT JOIN albums USING(item_id) LEFT JOIN album_images USING(album_id) LEFT JOIN images USING(image_id) WHERE tags.name='figurative';
Results image_id title alt filename filepath height width date_uploaded album_id item_id title description date_created title 1 image test.png 320 240 0000-00-00 00:00:00 1 1 a blank title for album 1 painting in progress 2013-09-27 13:45:02 a painting 1 image test.png 320 240 0000-00-00 00:00:00 2 1 a blank title for album 2 completed painting 2013-09-27 12:22:50 a painting 2 title testimg2.png 320 240 2013-09-22 10:06:42 2 1 a blank title for album 2 completed painting 2013-09-27 12:22:50 a painting 3 title2 alt test testimg3.png images/ 640 480 2013-09-30 20:03:52 2 1 a blank title for album 2 completed painting 2013-09-27 12:22:50 a painting 4 another image alt2 testimg4.png images/ 800 600 2013-09-30 20:03:52 3 2 another album 2013-09-27 12:45:32 another painting
JOINS
-
-
-
Use aggregate functions to select different values from a numeric column.
SELECT MIN(height), SUM(height), MAX(height) FROM images;
Results MIN(height) SUM(height) MAX(height) 320 2080 800 -
Get and sort by the total count of images in each album
SELECT COUNT(images.filename) as 'IMAGE COUNT', albums.title FROM images INNER JOIN album_images USING(image_id) INNER JOIN albums USING(album_id) GROUP BY albums.title ORDER BY `IMAGE COUNT`;
Results IMAGE COUNT title 1 a blank title for album 1 1 another album 3 a blank title for album 2
Using GROUP BY and Aggregate Functions
-
Control Flow Functions
Advanced Comparison Functions
FULLTEXT Searching
Optimization and EXPLAIN
TRANSACTIONS
Data Encryption
<?php require(CONNECT); include('includes/portfolio_db/portfolio_queries.php'); ?> <h2>Portfolio Database Table Data</h2> <?php if ($result = mysqli_query($link, 'SELECT * FROM portfolio_items;')) { results_to_table($result, 'Portfolio Items'); /* free result set */ mysqli_free_result($result); } if ($result = mysqli_query($link, 'SELECT * FROM categories;')) { results_to_table($result, 'Categories'); /* free result set */ mysqli_free_result($result); } if ($result = mysqli_query($link, 'SELECT * FROM tags;')) { results_to_table($result, 'Tags'); /* free result set */ mysqli_free_result($result); } if ($result = mysqli_query($link, 'SELECT * FROM item_tags;')) { results_to_table($result, 'Item Tags'); /* free result set */ mysqli_free_result($result); } if ($result = mysqli_query($link, 'SELECT * FROM albums;')) { results_to_table($result, 'Albums'); /* free result set */ mysqli_free_result($result); } if ($result = mysqli_query($link, 'SELECT * FROM album_images;')) { results_to_table($result, 'Album Images'); /* free result set */ mysqli_free_result($result); } if ($result = mysqli_query($link, 'SELECT * FROM images;')) { results_to_table($result, 'Images'); /* free result set */ mysqli_free_result($result); } ?> <h2>Advanced Query Examples</h2> <ol> <li> <ul class="examples"><h3>JOINS</h3> <li> <p>Get all images in all albums associated with the portfolio item with id=1</p> <pre><?php echo $q1;?></pre> <?php if ($result = mysqli_query($link, $q1)) { results_to_table($result); /* free result set */ mysqli_free_result($result); }?> </li> <li> <p>Get the number of albums associated with portfolio item 1</p> <pre><?php echo $q2;?></pre> <?php if ($result = mysqli_query($link, $q2)) { results_to_table($result); /* free result set */ mysqli_free_result($result); }?> </li> <li> <p>Get all portfolio item titles in the category 'paintings'</p> <pre><?php echo $q3;?></pre> <?php if ($result = mysqli_query($link, $q3)) { results_to_table($result); /* free result set */ mysqli_free_result($result); }?> </li> <li> <p>Use a self join to retrieve all the subcategories of the painting category.</p> <pre><?php echo $q4;?></pre> <?php if ($result = mysqli_query($link, $q4)) { results_to_table($result); /* free result set */ mysqli_free_result($result); }?> </li> <li> <p>Use a left join to retrieve all the tags of portfolio item 1.</p> <pre><?php echo $q5;?></pre> <?php if ($result = mysqli_query($link, $q5)) { results_to_table($result); /* free result set */ mysqli_free_result($result); }?> </li> <li> <p>Use a left join to retrieve all the images of portfolio items tagged with 'figurative'</p> <pre><?php echo $q7;?></pre> <?php if ($result = mysqli_query($link, $q7)) { results_to_table($result); /* free result set */ mysqli_free_result($result); }?> </li> </ul> </li> <li> <ul class="examples"><h3>Using GROUP BY and Aggregate Functions</h3> <li> <p>Use aggregate functions to select different values from a numeric column.</p> <pre><?php echo $q6;?></pre> <?php if ($result = mysqli_query($link, $q6)) { results_to_table($result); /* free result set */ mysqli_free_result($result); }?> </li> <li> <p>Get and sort by the total count of images in each album</p> <pre><?php echo $q8;?></pre> <?php if ($result = mysqli_query($link, $q8)) { results_to_table($result); /* free result set */ mysqli_free_result($result); }?> </li> </ul> </li> </ol> <h3>Control Flow Functions</h3> <h3>Advanced Comparison Functions</h3> <h3>FULLTEXT Searching</h3> <h3>Optimization and EXPLAIN</h3> <h3>TRANSACTIONS</h3> <h3>Data Encryption</h3> <?php require(DISCONNECT); ?>