我一直在为我的php脚本查询,但我不确定该怎么做。我对联接很糟糕,所以我不确定该怎么做。

我有两个表,项目和类别。

项目具有recno,sku,描述,价格,品牌,类别,详细信息,性别,大小,颜色,添加日期。类别具有recno,类别,父级。

我需要的查询必须选择类别为X且类别的父级为X的项目。

我试过了

SELECT DISTINCT items.recno,
                items.sku,
                items.description,
                items.price,
                items.brand,
                items.category,
                items.details,
                items.gender,
                items.size,
                items.color,
                items.dateadded
FROM   `items`
       INNER JOIN `categories`
               ON items.category = categories.parent
ORDER  BY `description`


但这只是选择了一切。我尝试使用联接,但始终无法从子类别中获取项目。

任何帮助,将不胜感激。

最佳答案

请尝试以下方法:

SELECT DISTINCT
    items.recno,
    items.sku,
    items.description,
    items.price,
    items.brand,
    items.category,
    items.details,
    items.gender,
    items.size,
    items.color,
    items.dateadded
FROM `items`
JOIN `categories` ON items.category = categories.parent
WHERE categories.category='x' AND categories.parent='X'


您没有在查询中添加WHERE条件,这就是为什么结果显示所有行的原因

关于php - MySQL查询问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16800473/

10-15 12:10
查看更多