explaination needed for "WHERE" command

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
itay
Forum Newbie
Posts: 4
Joined: Mon Sep 28, 2009 8:08 am

explaination needed for "WHERE" command

Post by itay »

Two tables in the database:

01. "entries". which includes the following fields: "id", "cat_id", "dateposted", "subject", "body"
02. "categories", which includes: "id", "cat"


now this query:

"SELECT entries.*, categories.cat FROM entries, categories
WHERE entries.cat_id = categories.id
..."

(It's taken from the book Practical PHP and Mysql by Jono Bacon, chapter 4)

can somebody give me a detailed explantion of what the WHERE is doing?
it's explained in the book, but not detailed enough for me to fully understand what is going on here.

thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: explaination needed for "WHERE" command

Post by John Cartwright »

It is very simple, it will only find results from the two tables "entries" and "categories" where the column "cat_id" from the "entries" table matches the column "id" in the "categories" table. Basically, it will join the two tables together based on those columns.

It can be more explicitly rewritten as

Code: Select all

SELECT entries.*, categories.cat FROM entries
INNER JOIN categories ON entries.cat_id = categories.id
itay
Forum Newbie
Posts: 4
Joined: Mon Sep 28, 2009 8:08 am

Re: explaination needed for "WHERE" command

Post by itay »

thank you! =]]
Post Reply