我有两张桌子:
[ product_to_category
---------------------
product_id
category_id
[ category
---------------------
category_id
parent_id
我需要使用category.parent_id为'39'来获取所有product_id。这就是我正在尝试的,但是当它至少有几百个时它会返回空白:
SELECT
product_id
FROM
product_to_category
WHERE
category_id IN (
SELECT parent_id FROM category WHERE parent_id = '39'
)
我在这里尝试做的是什么?
答案 0 :(得分:6)
知道了:
SELECT product_id FROM product_to_category INNER JOIN 类别ON product_to_category.category_id = category.category_id AND category.parent_id = 39
答案 1 :(得分:2)
试试这个:
SELECT product_id
FROM product_to_category p2c
Join category c
On c.category_id = p2c.category_id
Where parent_id = '39'
您在查询中的错误是:
SELECT product_id
FROM product_to_category
WHERE category_id IN
(SELECT category_id -- THIS IS WHERE YOUR SQL WAS INCORRECT
FROM category WHERE parent_id = '39')
答案 2 :(得分:0)
select product_id from product_to_category as ptc
inner join category as c
on ptc.category_id = c.parent_id
where c.parent_id = 39