php - 加入 where and less then 条件

标签 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'
";

我的 table 是

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 的行。

最佳答案

您需要使用 > 运算符而不是 >= 并使用 and 逻辑运算符而不是 or 为了防止出现 deleted = 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

关于php - 加入 where and less then 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30141361/

相关文章:

mysql - 如何从选择中创建具有给定列名的临时表?

MySQL 月和年分组性能低下

c# - 优化 LINQ 查询 - 如何缩短执行时间?

sql - 在 PSQL 脚本中使用环境变量

sql - 尽管缺少分析值,但完整的日期时间列表

php - mysqli 准备好的语句,使用绑定(bind)参数插入 NULL

php - 在php循环时显示两列html表格

php - 存储 Facebook 访问 token 的标准方法是什么?数据库还是cookie?

php - Laravel Eloquent 链式方法返回 true 而不是模型

mysql - django 的原始 SQL 语法有错误