tags:

views:

61

answers:

4

I have 3 tables,
- Section table that defines some general item sections.
- Category table -> has a "section" column (foreign key).
- Product table -> has a "category" column (foreign key).

I want to get all products that belong to X section.

How can I do it?

select from select?

A: 
select s.section, p.*
from section s
inner join category c on c.section = s.section
inner join product p on p.category = c.category
where s.section = 'section1'
RedFilter
A: 
select p.*
from Product p
join Category c on p.CategoryId = c.Id
join Section s on c.SectionId = s.Id
where s.Id = @val
roufamatic
+4  A: 
Select 
   prod.*
FROM
   Product prod
   INNER JOIN Category cat ON prod.category = cat.id
   INNER JOIN Section sec ON cat.section = sec.id
WHERE
   sec.id = X
Mike Burton
A: 

Lots of same answers here. For some reason, though, all of them are joining the Section table which is (likely) not necessary.

select
  p.*

from
  Product    p,
  Category   c

where
  p.category_id = c.id and
  c.section_id = 123
;

Explicit ANSI JOIN syntax per @nemiss's request:

select
  p.*

from Product    p

join Category   c
  on c.id = p.category_id
 and c.section_id = 123
;

Possible reason to include Section table: Selecting products based on Section name (instead of ID).

select
  p.*

from Product    p

join Category   c
  on c.id = p.category_id

join Section    s
  on s.id = c.section_id
 and s.name = 'Books'
;

If doing this, you'll want to make sure Section.name is indexed

alter table Product add index name;
macek
yes! section is not necessary!! it's just defines info about the section but doesn't contribute to my query!
nemiss
I don't like the use of the multi from (Form table1, table2) can it be changed to use join instead?
nemiss
You don't HAVE to join to section, but typically you don't directly query based on an id, but rather some characteristic of the row. Your query is shorter but much less flexible. It also suffers if the FK relationship is not explicitly enforced in the database, since in that case c.section_id could contain a nonexistent value.
Mike Burton
@Mike Burton, the OP says he is using FKs.
macek
He does say that. It doesn't necessarily make it true in the formal sense. I know developers who include "informal" table links in their definition of FK. It also doesn't mean he's using a DBMS that does a good job of managing FKs (there are several older systems where you can force-commit invalid legacy data). Moreover, my main point was that the query ignores the explicit relationship in favour of a magic number, which is bad code design in almost all cases.
Mike Burton
@nemiss, I update my answer to provide the `INNER JOIN` syntax.
macek
@Mike Burton, if the `Category.section_id` is not not valid, it won't be able to join the `Section` table anyway. The only reason to join the `Section` table would be if we were filtering on a column we didn't join on, e.g., `Section.name`.
macek
I already said that. The difference being, you'd get no results if you were joining to the section table, whereas here you'd get a result, and that result would not be valid for the schema as defined.
Mike Burton