如何 select * 从 table where col1+col2=b using active record

how to select * from table where col1+col2=b using active record

如何运行 select * from table where col1+col2=b; 使用 codeigniter 的活动记录。使用 mysql

我试过了但是失败了

$this->db->where("col1 + col2",$b)->get("table")->result_array();

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '915087' at line 4

SELECT * FROM table WHERE a + b 25

 $this->db->select('(col1+col2) as col3');
         $this->db->from('table');
         $this->db->HAVING('col3 = ', $b);
         $query = $this->db->get();

试试这个。

Hope it will solved your problem. If you have any further query you can knock me feel free.

$this->load->model('dummy_table_model');
$b = 25;
$data = $this->db->get_where("dummy_table", array("(number1 + number2) = " => $b))->result_array();
// $this->db->last_query();
var_dump($data);


// Out put 
array (size=4)
  0 => 
    array (size=4)
      'id' => string '1' (length=1)
      'number1' => string '10' (length=2)
      'number2' => string '15' (length=2)
      'number3' => string '25' (length=2)
  1 => 
    array (size=4)
      'id' => string '2' (length=1)
      'number1' => string '23' (length=2)
      'number2' => string '2' (length=1)
      'number3' => string '25' (length=2)
  2 => 
    array (size=4)
      'id' => string '3' (length=1)
      'number1' => string '9' (length=1)
      'number2' => string '16' (length=2)
      'number3' => string '25' (length=2)
  3 => 
    array (size=4)
      'id' => string '4' (length=1)
      'number1' => string '23' (length=2)
      'number2' => string '2' (length=1)
      'number3' => string '25' (length=2)

You can do that how you were thought. Just fixed out your mistake follow the below code. Thanks. If it's useful please acknowledge it.

    $b = 25;
    $data = $this->db->where(array("(number1 + number2) = " => $b))->get("dummy_table")->result_array();
    echo "<pre>";
    print_r($data);



// output 
Array
(
    [0] => Array
        (
            [id] => 1
            [number1] => 10
            [number2] => 15
            [number3] => 25
        )

    [1] => Array
        (
            [id] => 2
            [number1] => 23
            [number2] => 2
            [number3] => 25
        )

    [2] => Array
        (
            [id] => 3
            [number1] => 9
            [number2] => 16
            [number3] => 25
        )

    [3] => Array
        (
            [id] => 4
            [number1] => 23
            [number2] => 2
            [number3] => 25
        )

)