Can someone please explain what is going on here?
mysql> SELECT cat.id, cat.name, cat.description
-> FROM categories cat, categories parent
-> WHERE cat.parent_id = parent.id
-> AND parent.name = ‘Fruits’;
The table’s colums are
±—±----------±---------±--------------------------------+
| id | parent_id | name | description |
±—±----------±---------±--------------------------------+
Where does the cat.id, and categories cat, categories parent and all that come into it?
This is an exert from the following:
***** START EXERT *****
Okay, now let’s see what we have again:
mysql> SELECT * FROM categories;
±—±----------±---------±--------------------------------+
| id | parent_id | name | description |
±—±----------±---------±--------------------------------+
| 0 | 0 | Top | This is the top level category. |
| 1 | 0 | Fruits | Fresh and tasty fruits. |
| 2 | 0 | Snacks | Tasty snacks. |
| 3 | 1 | Apples | Yummy crunchy apples. |
| 4 | 1 | Berries | Yummy berries. |
| 5 | 2 | Chips | Crunchy Greasy Treats. |
| 6 | 2 | Icecream | Great on a hot summer day. |
±—±----------±---------±--------------------------------+
Let’s practice writing some more interesting SELECT queries. For example, let’s find all the sub-categories under the [Fruits] category:
mysql> SELECT cat.id, cat.name, cat.description
-> FROM categories cat, categories parent
-> WHERE cat.parent_id = parent.id
-> AND parent.name = ‘Fruits’;
±—±--------±----------------------+
| id | name | description |
±—±--------±----------------------+
| 3 | Apples | Yummy crunchy apples. |
| 4 | Berries | Yummy berries. |
±—±--------±----------------------+
Note that was a little unnecessary because we already knew that the ID of the [Fruits] category was 1, we could have just run the query:
mysql> SELECT * FROM categories WHERE parent_id = 1;
to do the same thing, but that was for fun so that we could practice our joins
**** END EXERT ****
Which comes from the website
http://www.devshed.com/c/a/PHP/Building … Catalog/5/