i am trying to fetch data from database table in between two dates and it is having two more filter using 'OR' clause but in result i am getting data either between date or data fetched using 'OR' clause.I am posting here query in model if anyone knows solution please let me know.
Thanks in advance
$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$this->db->where( 'against = Recharge');
$this->db->or_where('against =','Rech_Commission');
$query = $this->db->get();
return $query->result();
I want date filter compulsory but want to fetch records against=Recharge or against='Rech_commission'but i am getting either using date range or against='Rech_Commission'
use
$where = '(against="Recharge" or against = "Rech_Commission")';
$this->db->where($where);
and you had date and date and (against = '' or against = '')
$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->where('date BETWEEN "'. date('Y-m-d', strtotime($startDate)). '" and "'. date('Y-m-d', strtotime($endDate)).'"');
$this->db->where('against="Recharge" OR against = "Rech_Commission")';
$query = $this->db->get();
return $query->result();
$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->group_start();
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$this->db->where( 'against = Recharge');
$this->db->group_end();
$this->db->or_where('against =','Rech_Commission');
$query = $this->db->get();
return $query->result();
Related
Im trying to get values of 'yrlvl' with 11 or 1112 values but i cant get it whit this line of code:
$this->db->order_by('id', 'DESC');
$query = $this->db->get_where('subjects', array('strand' => 'GAS', 'yrlvl' => '11'or '1112'));
return $query->result_array();
I'm only getting the 1112 value someone help please.
Here are some possible solutions:
Solution 1:
$this->db->select('*');
$this->db->from('subjects');
$this->db->where('strand', 'GAS');
$where = '(yrlvl = "11" or yrlvl = "1112")'; // make a OR statement by passing values here
$this->db->where($where);
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
return $query->result_array();
Solution 2:
$this->db->select('*');
$this->db->from('subjects');
$this->db->where('strand', 'GAS');
$where = array('11', '1112'); // make an array of values for OR condition
$this->db->where_in('yrlvl', $where);
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
return $query->result_array();
I want to convert the following SQL query to codeIgniter active records format
SELECT * FROM `relation`
WHERE (`user1` = 144 OR `user2` = 144)
AND `status` = 2
AND `action` != 1
My attempt was something like this,
$ID=144;
$this->db->where('user1', $ID);
$this->db->or_where('user2', $ID);
$this->db->where('status', 2);
$this->db->where('action!=', 1);
However the results are not compatible. Can you point what is off here?
I would also be fine with suggestions not specifically using active records for the query.
the correct query would be
$query = $this->db
->select('*')
->from('relation')
->group_start()
->where('user1', $ID)
->or_where('user2', $ID)
->group_end()
->where('status', 2)
->where('action !=', 1)
->get();
As your are using OR between multiple condition you must have to enclosed this in bracket. Change your code as below:
$this->db->where('(user1', $ID,FALSE);
$this->db->or_where("user2 = $ID)",NULL,FALSE);
$this->db->where('status', 2);
$this->db->where('action!=', 1);
Hope this will help you :
$ID = 144;
$this->db->select('*');
$this->db->from('relation');
$this->db->where('user1', $ID);
$this->db->or_where('user2', $ID);
$this->db->where('status', '2');
$this->db->where('action !=', '1');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
print_r($result);
//return $query->result();
}
For more : https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data
There is no result even I have data in the database:
$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('roleID',"4");
$query = $this->db->get();
return $query->result();
In joining you have to define the table name with where condition.Like this...
$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('table_name.roleID',4);//table_name is name of table having column roleID
$query = $this->db->get();
return $query->result();
$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('roleID',"4");
$query = $this->db->get();
return $query->result();
your query work with one table data when you join another table you can must define $this->db->where('user.roleID',4) like this.
you can write this query like this for when you get the join table data
$this->db->select('user.*,userprofile.youdesirename,classroom.class name');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('user.roleID',4)
$query = $this->db->get();
return $query->result();
I am trying to convert this query:
SELECT * FROM table WHERE condition1 = 'example' AND (date1 = 'date' OR renewal1 = 'renewal');
into CodeIgniter's Active Record format. I tried the following:
$q = $this->db->select('*');
$q = $this->db->from('table');
$q = $this->db->where('condition1 = condition');
$q = $this->db->where('date1 = date OR renewal = renewal');
$q = $this->db->get();
$results = $q->result();
But this did not have the desired affect. It seemed to not place the parenthesis around the second set of conditions, which caused the query to not work as expected. How else can I do this to represent what I want?
Thanks for the help!
You can use
$this->db->select('*');
$this->db->from('table');
$this->db->where('condition1 =',' condition');
$where = '(date1 = "date" OR renewal1 = "renewal")';// you can use your condition like this
$this->db->where($where);
$q = $this->db->get();
$results = $q->result();
I'm trying to...
Set some query parameters and filters.
Get the total number of matching rows.
Set limit and offset.
Get the results
Here's what I initially tried:
$this->db->select(<aggregation, subqueries>);
$this->db->from('users');
$this->db->where(<complex filters>);
$total = $this->db->count_all_results();
$this->db->limit($limit, $offset);
$query = $this->db->get();
$result = $query->result();
But calling count_all_results calls _reset_select internally meaning I have to do the first step again - not very DRY.
How would you achieve this in a simple, clean way?
Just use Query Builder Caching
$this->db->start_cache();
$this->db->select(<aggregation, subqueries>);
$this->db->from('users');
$this->db->where(<complex filters>);
$this->db->stop_cache();
$total = $this->db->count_all_results();
$this->db->limit($limit, $offset);
$query = $this->db->get();
$this->db->flush_cache();
$result = $query->result();
https://codeigniter-30-dev.readthedocs.io/zh_TW/latest/database/query_builder.html#this-db-count-all-results