【问题标题】:how to show up a value that is closest to or equal to the results of calculating the system with the database?如何显示一个最接近或等于系统与数据库计算结果的值?
【发布时间】:2019-02-24 05:59:31
【问题描述】:

我使用框架 CI

我在数据库中有两个表。第一个表是“A”,第二个表是“B”。在“A”表中有一个“DIAMETER”列,其中包含用户从系统计算中获得的数字。

在“B”表中有一个“标准”列,其中包含管理员输入的数字。

那么如何在用户计算出的“DIAMETER”数接近或等于“STANDARD”时显示“STANDAR”数?

In the database

表 A,列 直径 2.5、3.8、 5.6

表 B,列 标准 3、 3.5, 4、 5、 6、

如果计算直径3.8的结果是要提升的结果是4,如果结果是直径5.6那么结果是提升6。 我期望的结果接近标准数或与标准数相同的顶级数字

我的模型

public function get_standar(){
$this->db->order_by('standar', 'ASC');
$this->db->limit('0');
return $this->db->get('B')->result();

}

我的观点

<div class="form-group has-success">
<label>Diameter</label>
<div class="form-group input-group">
    <input class="form-control" name="diameter" type="text" value="<?php echo number_format($getdata->diameter,2);?>" readonly="">
    <span class="input-group-addon">mm</span>
</div>
<label>Standar</label>
<select class="form-control" name="standar" id="standar">
<?php foreach ($standar as $stand) {?>
    <option <?php echo $standar_selected == $stand->id_standar ? 'selected="selected"' : '' ?> value="<?php echo $stand->id_standar ?>">
        <?php if ($getdata->diameter >= $stand->standar) { echo $stand->standar ;} ?>
    </option>
<?php }?>
</select>

enter image description here enter image description here

【问题讨论】:

    标签: php mysql codeigniter codeigniter-3 codeigniter-2


    【解决方案1】:

    以下查询返回大于或等于您计算的直径的最小标准直径:

    $diameter = 3.8;
    
    $sql = "SELECT MIN(column_standard), {other columns you need}
    FROM table B 
    WHERE column_standard >= $diameter";
    
    $this->db->query($sql);
    

    上面的查询返回 4 作为结果。

    【讨论】:

    • 谢谢你的回答,在我使用框架codeigniter之前,这个可以用吗?
    • 当然,如果您在 mysql 客户端上运行此查询,它将正常工作。您只需要将 $diameter 替换为 mysql 客户端上的值。
    • 你得到了什么错误之王?你能提供一些与你查询相关的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    相关资源
    最近更新 更多