Php 从数据库检索数据的SQL查询

Php 从数据库检索数据的SQL查询,php,mysql,Php,Mysql,我根据项目的位置id和数量从数据库中检索超过五个项目,但我希望针对特定位置id检索数量超过0个项目的项目 这是我的疑问: SELECT DISTINCT(warehouses.item_id), SUM(warehouses.qty) AS qty, items.gender_id, items_dept.desc, items_dept.dept_id, items.msrp,items.rtp FROM `warehouses` JOIN `items` ON ite

我根据项目的位置id和数量从数据库中检索超过五个项目,但我希望针对特定位置id检索数量超过0个项目的项目

这是我的疑问:

SELECT DISTINCT(warehouses.item_id), SUM(warehouses.qty) AS qty, items.gender_id, items_dept.desc, items_dept.dept_id, items.msrp,items.rtp
FROM `warehouses`
    JOIN `items`
        ON items.item_id = warehouses.item_id
    JOIN `items_dept`
        ON items_dept.dept_id = items.dept_id
WHERE items.gender_id IN (3,5,6)
AND qty > 5
AND warehouses.wrhs_id IN (20,21,22,23,24,25,26,27,28,29,30,31,32,38,40,44,47,49,55,60,61)
AND items.dept_id = :catId
AND items.rtp BETWEEN :priceX AND :priceY
GROUP BY warehouses.item_id
ORDER BY warehouses.item_id ASC

我希望wrhs_id(44)的数量不超过5但超过0,在SQL中,您可以使用LIMIT param指定返回项目的限制,但如果检索到0个项目,则会出错或数据库中没有数据,请尝试以下操作:

SELECT DISTINCT(warehouses.item_id), SUM(warehouses.qty) AS qty, items.gender_id, items_dept.desc,items_dept.dept_id,items.msrp,items.rtp
            FROM `warehouses`
            JOIN `items`      ON items.item_id      = warehouses.item_id
            JOIN `items_dept` ON items_dept.dept_id = items.dept_id
            WHERE items.gender_id IN (3,5,6)
            AND qty > 0
            AND warehouses.wrhs_id = 44
            AND items.dept_id = :catId
            AND items.rtp BETWEEN :priceX AND :priceY
            GROUP BY warehouses.item_id
            ORDER BY warehouses.item_id ASC

SQL代码?SQL架构?否我希望在查询中检索wrhs_id(1,2,3,4)中的项,且其数量必须大于5项;检索wrhs_id(5)中的项,且其数量必须大于0项
SELECT DISTINCT(warehouses.item_id), SUM(warehouses.qty) AS qty, items.gender_id, items_dept.desc, items_dept.dept_id, items.msrp,items.rtp
FROM `warehouses`
    JOIN `items`
        ON items.item_id = warehouses.item_id
    JOIN `items_dept`
        ON items_dept.dept_id = items.dept_id
WHERE items.gender_id IN (3,5,6)
AND ((
        qty > 5
        AND
        warehouses.wrhs_id IN (20,21,22,23,24,25,26,27,28,29,30,31,32,38,40,47,49,55,60,61)
    )
    OR (
        qty > 0
        AND
        warehouses.wrhs_id = 44
    )
)
AND items.dept_id = :catId
AND items.rtp BETWEEN :priceX AND :priceY
GROUP BY warehouses.item_id
ORDER BY warehouses.item_id ASC