我有三张桌子,
定义一些一般项的类别表有一个“
Section”列( sections.Category key).Product表->有一个“->”列(外键)。我想要得到所有属于X部分的产品。
我该怎么做呢?
从select选择?
发布于 2010-04-07 07:15:04
这里有很多相同的答案。然而,由于某种原因,它们都加入了Section表,这(很可能)是不必要的。
select
p.*
from
Product p,
Category c
where
p.category_id = c.id and
c.section_id = 123
;根据@nemiss的请求,显式ANSI JOIN语法:
select
p.*
from Product p
join Category c
on c.id = p.category_id
and c.section_id = 123
;包含Section表的可能原因:根据部分名称(而不是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'
;如果执行此操作,则需要确保Section.name已被索引
alter table Product add index name;发布于 2010-04-07 05:04:11
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发布于 2010-04-07 05:03:12
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'https://stackoverflow.com/questions/2588304
复制相似问题