获取最低列值高于最小阈值的数据库表行

问题描述 投票:0回答:3

我正在开发 codeigniter 电子商务网站。我有一张下表

shipping table

product_weight (in kgs)  shipping_charge
1.000                    60
1.500                    90
2.000                    120
2.500                    150
3.000                    180
3.500                    210
4.000                    240

我想查找添加到购物车的产品的运费。但我没能计算出来。如果产品重量为 2.800,那么我应该得到 Shipping_charge 值 180。我不知道该怎么做。下面是我的代码

function get_shipping_charge(){
    $this->db->select('shipping_charge');
    $this->db->from('shipping_table');
    $this->db->where('product_weight >=', $product_weight);
    $this->db->limit('1');
    $query = $this->db->get();
    return $query->result();
}
php sql codeigniter
3个回答
1
投票
function get_shipping_charge(){
$this->db->select('shipping_charge');
$this->db->from('shipping_table');
$this->db->where('product_weight >=', $product_weight);
$this->db->order_by("product_weight","ASC");
$this->db->limit('1');
$query = $this->db->get();
return $query->result();
}

0
投票

试试这个:

function get_shipping_charge() {
    $this->db->select('MIN(shipping_charge) AS shipping_charge');
    $this->db->from('shipping_table');
    $this->db->where('product_weight >=', $product_weight);
    $query = $this->db->get();
    return $query->result();
}

0
投票

这对你有用吗?

function get_shipping_charge() {
    $this->db->select('IFNULL(MAX(shipping_charge), (SELECT MIN(shipping_charge) FROM shipping_table))');
    $this->db->from('shipping_table');
    $this->db->where('product_weight <=', $product_weight);
    $query = $this->db->get();
    return $query->result();
}

注意

更新查询以处理小于 1.000 的重量值。

© www.soinside.com 2019 - 2024. All rights reserved.