来自2个表的Sqlite select语句

时间:2016-02-17 15:50:35

标签: sql sqlite

我有两张桌子:ProductsCategoryProducts CategoryProducts包含:

PrdID    Category
-----------------  

Products包含:

PrdID   Barcode   Url
-----------------------

我有产品的条形码值,例如111111.

  

需要选择具有相同类别的所有Products.Url,如此项目   分类

拥有Products.PrdID,我可以从CategoryProducts获得所有需要的PrdID:

select distinct c1.PrdID
from CategoryProduct_MM c1
where c1.CategoryID in (select c2.CategoryID
                        from CategoryProduct_MM c2
                        where c2.PrdID = 175)

3 个答案:

答案 0 :(得分:1)

SELECT 
    p.url
FROM
    Products p
JOIN
    CategoryProducts cp ON cp.PrdID = p.id
// WHERE p.id = 175
GROUP BY p.url, p.Category

如果您需要添加评论查询

答案 1 :(得分:1)

根据您的评论,这就是您想要的:

SELECT t.Url
FROM Products t
INNER JOIN CategoryProducts s
ON(s.PrdID = t.PrdID)
WHERE s.CategoryID = (select p.categoryID from CategoryProducts p
                      INNER JOIN Products f ON(p.prdID = f.prdID)
                      WHERE f.barcode =  42244)

选择所有网址,其用户与PrdID属于同一类别 - > 175

答案 2 :(得分:1)

 SELECT p.Url, p.PrdID
     FROM Products p 
JOIN CategoryProduct cp 
    ON p.PrdID = cp.PrdID 
JOIN 
Category c on c.id = cp.CategoryID AND cp.PrdID =175

//你需要加入表类别 告诉我你的分类表结构