I have 2 tables:
product_image (contains columns = product_image_id, product_id, image)
and
product_to_category (contains columns = product_id, category_id)
All I want to do is select the image field in the first table for every record that has a category id of 35 in the second table.
Thanks...
I cannot figure out a real easy query using 2 tables
Moderator: General Moderators
- andyhoneycutt
- Forum Contributor
- Posts: 468
- Joined: Wed Aug 27, 2008 10:02 am
- Location: Idaho Falls
Re: I cannot figure out a real easy query using 2 tables
This should do the trick...
-Andy
Code: Select all
SELECT i.image FROM product_image i
JOIN product_to_category c ON i.product_id = c.product_id
WHERE c.category_id = 35;Re: I cannot figure out a real easy query using 2 tables
That worked perfectly - thanks very much!
What do the i. and c. mean?
What do the i. and c. mean?
Re: I cannot figure out a real easy query using 2 tables
Aliases for table names - you could also use full table names instead: product_image. and product_to_category.mikeman wrote:What do the i. and c. mean?
- andyhoneycutt
- Forum Contributor
- Posts: 468
- Joined: Wed Aug 27, 2008 10:02 am
- Location: Idaho Falls
Re: I cannot figure out a real easy query using 2 tables
If you look at the query, you can see that I'm selecting from product_image i and joining on product_to_category c. The "i" and "c" are just aliases, or short-hand notation, for the tables that we're looking at. In this case, I used "i" for "image" and "c" for "category". Does that make sense?
Code: Select all
SELECT i.image FROM product_image i
JOIN product_to_category c ON i.product_id = c.product_id
WHERE c.category_id = 35;Re: I cannot figure out a real easy query using 2 tables
to make it clearer, common practice is to use 'AS' between the table/column and its alias.
Code: Select all
product_image AS i ...
product_to_category AS c- andyhoneycutt
- Forum Contributor
- Posts: 468
- Joined: Wed Aug 27, 2008 10:02 am
- Location: Idaho Falls
Re: I cannot figure out a real easy query using 2 tables
Good call. I'm too lazy sometimes!pytrin wrote:to make it clearer, common practice is to use 'AS' between the table/column and its alias.Code: Select all
product_image AS i ... product_to_category AS c