duanliaogui4133 2014-03-02 10:32
浏览 652
已采纳

使用in()的mysql查询不起作用

I am trying to find all products that are connected with category list with this query:

SELECT p.id, product_name, added_date 
FROM products p, products_to_categories ptc 
WHERE ptc.category_id in ("89,88,83,87,84,85,86,82") 
AND ptc.product_id=p.id and p.active="1" 
group by p.id   

if I cut the condition AND ptc.product_id=p.id returns rows - wrong rows for active condition.. what is the correct way to get correct information with 1 query? - is it possible at all? thanks

  • 写回答

3条回答 默认 最新

  • dongpu3792 2014-03-02 10:35
    关注

    Don't quote the list of category IDs:

    SELECT p.id, product_name, added_date 
    FROM products p
    JOIN products_to_categories ptc ON ptc.product_id=p.id
    WHERE ptc.category_id IN (89, 88, 83, 87, 84, 85, 86, 82) 
    AND p.active="1" 
    GROUP BY p.id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?