Page 1 of 1

explaination needed for "WHERE" command

Posted: Sat Oct 03, 2009 1:05 pm
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

Re: explaination needed for "WHERE" command

Posted: Sat Oct 03, 2009 1:13 pm
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

Re: explaination needed for "WHERE" command

Posted: Sat Oct 03, 2009 1:25 pm
by itay
thank you! =]]