如何使用活动记录

时间:2016-12-20 08:29:01

标签: php mysql codeigniter activerecord mariadb

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

我试过了但是失败了

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

您的SQL语法有错误;查看与您的MariaDB服务器版本相对应的手册,以便在#91; 915087'附近使用正确的语法。在第4行

     

SELECT * FROM table WHERE a + b 25

3 个答案:

答案 0 :(得分:1)

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

试试这个。

答案 1 :(得分:1)

  

你可以这样做,你的想法。只是解决了你的错误   请按照以下代码。谢谢。如果它有用,请确认。

    $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
        )

)

答案 2 :(得分:0)

  

希望它能解决你的问题。如果你有任何进一步的询问,你可以让我感到自由。   enter image description here

$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)