【问题标题】:Can I use somename from select field as somename in where clause?我可以在 where 子句中使用 select 字段中的 somename 作为 somename 吗?
【发布时间】:2013-01-04 08:58:24
【问题描述】:

我正在尝试获取 totalQuantity 小于 alterQuantity 的产品。

$this->db
->select("products.id as productid, products.code as code, products.name, products.unit, products.price, sum(warehouses_products.quantity) as totalQuantity, products.alert_quantity as alertQuantity")
->from('products')
->where('alertQuantity >=', 'totalQuantity')
->join('warehouses_products', 'products.id=warehouses_products.product_id, 'left')
->group_by("products.id");

如果我在 where 子句中使用 products.alert_quantity 的 alertQuantity 我得到错误 Unknown column 'alertQuantity' in 'where Clause' 这个我不知道如何改变,因为这是数量的总和。你能帮我解决这个问题吗?

我的问题: 我选择 alert_quantity 作为 alertQuantity 和 sum(quantity) 作为 totalQuantity

我可以在 where 子句中使用 alterQuantity 和 totalQuantity 吗?

表结构

产品

id、代码、名称、单位、价格、alert_quantity

仓库产品

id、product_id、warehouse_id、数量


找到 我不能使用 totalQuantity 有没有其他想法可以将 products.alert_quantity 与 sum(warehouses.products.quantity) 进行比较?

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    试试这个::

    select 
    products.id as productid, 
    products.code as code, 
    products.name, products.unit, 
    products.price, 
    sum(products.alert_quantity) as totalQuantity, 
    products.alert_quantity as alertQuantity
    from products
    
    LEFT join warehouses_products on products.id=warehouses_products.product_id
    where products.alert_quantity>= totalQuantity
    group_by products.id
    

    【讨论】:

    • 您的 SQL 语法有误;检查与您的 MySQL 服务器版本相对应的手册,以在 'group_by products.id 附近使用正确的语法) ORDER BY (select productid, code, products.name, `'
    • 在我的查询中,GROUP by Products.id 后面没有任何右括号,请粘贴整个查询以及 order by 子句
    • 对不起,这是错误 - 'where 子句'中的未知列 'alertQuantity' 选择 products.id 作为 productid,products.code 作为代码,products.name,products.unit,products.price,sum( warehouses_products.quantity) 作为 totalQuantity,products.alert_quantity 作为 alertQuantity 从 products LEFT 加入 warehouses_products on products.id=warehouses_products.product_id where alertQuantity >= totalQuantity GROUP BY products.id
    • 请粘贴products表的表结构
    • totalQuantity is unknown 我发现这个不能用了。
    【解决方案2】:

    尝试使用 select 的第二个参数,例如 改变

    $this->db
    ->select("products.id as productid, products.code as code, 
    products.name, products.unit, products.price, 
    sum(warehouses_products.quantity) as totalQuantity, 
    products.alert_quantity as alertQuantity")
    

    $this->db
    ->select("products.id as productid, products.code as code, 
    products.name, products.unit, products.price, 
    sum(warehouses_products.quantity) as totalQuantity, 
    products.alert_quantity as alertQuantity", FALSE)
    

    并关闭连接部分中的引号

    编辑,尝试重新排序您的语句,例如:

    $this->db
    ->select("products.id as productid, products.code as code, products.name, products.unit, products.price, sum(warehouses_products.quantity) as totalQuantity, products.alert_quantity as alertQuantity", FALSE)
    ->from('products')
    ->join('warehouses_products', 'products.id=warehouses_products.product_id', 'left')
    ->where('alertQuantity >=', 'totalQuantity')
    ->group_by("products.id");
    

    【讨论】:

    • 在“where 子句”中尝试了相同的错误未知列“alertQuantity”
    • @Saleem 尝试更改您的 where 子句,如答案中添加的代码
    • 已经试过了 :( 但同样的问题是它不允许我使用我在 select as 中使用的 alertQuantiy 和 totalQuantity
    • @Saleem,不确定,但您可以尝试重新排序您的语句,例如 select --> from --> join --> where ... 就像我编辑的答案中一样
    • 我尝试了所有,发现SQL是从右到左向后评估的。因此 where 子句在 select 子句之前被解析和评估。因此,u_name 到 user_name 的别名尚未发生。 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 2016-12-28
    • 2017-07-18
    • 2017-05-07
    • 2012-04-28
    • 2013-02-20
    • 2011-02-14
    相关资源
    最近更新 更多