MySQL查询的问题

时间:2013-05-28 20:02:39

标签: php mysql

我一直在研究我的php脚本的查询,我不知道如何做我想要的。加入时我很糟糕,所以我不确定我是怎么做的。

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

项目有recno,sku,描述,价格,品牌,类别,细节,性别,尺寸,颜色,日期添加。类别有recno,category,parent。

我需要的查询必须选择类别为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` 

但这只是选择一切。我尝试使用连接,但从未能够从子类别中获取项目。

对此的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

请试试这个:

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条件,这就是结果显示所有行的原因