加入where where和less then condition

时间:2015-05-09 14:51:20

标签: php mysql sql database select

这是我的问题:

$sql = "
    SELECT
        items.item_id,
        name,
        category,
        item_number,
        cost_price,
        unit_price,
         item_quantities.quantity
    FROM ospos_items
    LEFT JOIN item_quantities ON items.item_id = item_quantities.item_id
    WHERE
        deleted='0' OR item_quantities.quantity>='0'
";

我的桌子是

Item table
     Name   | item_id | Cost price | Unit price | 
    shirt1  |   4     |  3800.00   | 5320.00    
    shirt2  |   5     |  3800.00   | 5320.00
    shirt3  |   6     |  3800.00   | 5320.00
    shirt5  |   7     |  2900.00   | 4060.00

数量表

item_id | Quantity
4       |   5      
5       |   4
6       |   3
7       |   0

我不想显示数量为0的行。

3 个答案:

答案 0 :(得分:1)

您需要使用erl -pa ebin deps/*/ebin运算符代替>并使用>=逻辑运算符代替and,以防止ordeleted = 0quantity = 0出现:

SELECT    items.item_id, 
          name,
          category,
          item_number,
          cost_price,
          unit_price,
          item_quantities.quantity 
FROM      ospos_items 
LEFT JOIN item_quantities ON items.item_id = item_quantities.item_id
WHERE     deleted = 0 AND item_quantities.quantity > 0

答案 1 :(得分:0)

您可以尝试以下查询:

select A.Name, A.item_id, A.Cost_price, A.Unit_price
from Item A
INNER JOIN Quantities B
ON A.item_id = B.item_id where B.Quantity > 0

答案 2 :(得分:0)

根据您的表格,您应该写: 在加入子句中ospos_items.item_id而不是items.item_id。
我删除了where语句中的字段删除,因为你的表中没有提到它。

SELECT
    *
FROM ospos_items
LEFT JOIN item_quantities ON ospos_items.item_id = item_quantities.item_id
WHERE
    item_quantities.quantity > 0
;
相关问题