i want to get data using where in condition
my code is below
SELECT `cuc_UserCardsDetail`.*, `cuc_CreditCardType`.`varValidName`
FROM (`cuc_UserCardsDetail`)
JOIN `cuc_CreditCardType` ON `cuc_CreditCardType`.`intGlCode` =
`cuc_UserCardsDetail`.`fk_CardTypeGlCode`
WHERE `cuc_UserCardsDetail`.`intGlCode` IN ('10,29')
AND `chrAccountType` = 'M'
ORDER BY `dtCreateDate` desc
now I have resulted with one row actually in my table there is two-row with related id
i think problem is In('10,29') but i want like ('10','29') or (10,29) how can i do in codeigitor?
i want (10,29) or ('10','29') in where in ..
In CI query builder use
$id= array('10', '20'); # or direct assing your input params
$this->db->where_in('column_name', $id);
Read Looking for Specific Data - Codeigniter.com
Method chaining will accept array in where_in
$intGlCode = array(10, 29);
$this->db->select('`cuc_UserCardsDetail`.*, `cuc_CreditCardType`.`varValidName`');
$this->db->from('cuc_UserCardsDetail');
$this->db->join('cuc_CreditCardType','cuc_CreditCardType.intGlCode =
cuc_UserCardsDetail.fk_CardTypeGlCode');
$this->db->where('chrAccountType','M');
$this->db->where_in('cuc_UserCardsDetail.intGlCode',$intGlCode);
$this->db->order_by('dtCreateDate','DESC');
$data = $this->db->get()->result_array();
Your query look something like
you have to define column chrAccountType from which table, here i assume it from cuc_UserCardsDetail
$this->db->select('ud.*, cc.varValidName');
$this->db->where_in('ud.intGlCode', ['10','29']);
$this->db->where('ud.chrAccountType', 'M');
$this->db->order_by('dtCreateDate', 'desc');
$this->db->join('cuc_CreditCardType cc', 'cc.intGlCode = ud.fk_CardTypeGlCode');
$data = $this->db->get('cuc_UserCardsDetail ud')->result_array();
print_r($data);
where_in() function just in the case of array of data.
$ids = array();
$this->db->where_in('id', $ids);
Related
I have a table, myTable, with a single column, myColumn, with a single row.
I want to change the value of myColumn in that first (and only) row.
I tried this, but nothing happens:
$myNewValue = 'foo';
$this->db->update('myColumn', $myNewValue);
What am I doing wrong?
Obviously it must have something to do with not specifying which table, but I don't know how to do that.
Codeigniter's update() needs to follow this syntax:
update([$table = ''[, $set = NULL[, $where = NULL[, $limit = NULL]]]])
Parameters:
$table (string) – Table name
$set (array) – An associative array of field/value pairs
$where (string) – The WHERE clause
$limit (int) – The LIMIT clause
so besides of adding the correct table name, you'd need to send update data as an array, using this approach:
$myNewValue = array('myColumn'=>'foo');
$this->db->update('myTable', $myNewValue);
Figured out how to do it:
$this->db->update('myTable', [
'myColumn' => $myNewValue,
]);
Try this method, it is my favorite format.
$where = array(
'column_id'=$id
);
$data = array(
'column_name'=$data
);
$this->db->update('table_name', $data,$where);
How can I get only the first record using multiple IDs in Laravel? In the following code, $ids has three elements. I want to fetch the first record from table using these three IDs.
$ids = [16, 18, 20];
$listing = Listing::whereIn('id', $ids)->first();
If you want to get the first object with smallest id, you can do:
$listing = Listing::whereIn('id', $ids)->oldest('id')->first();
But better to do this:
$listing = Listing::find(min($ids));
If you want to get an object with the first ID in the array, do this:
$listing = Listing::find($ids[0]);
You can use either limit or oldest function of laravel see below query
$listing = Listing::whereIn('id', $ids)->limit(1)->get(); //using limit
OR
$listing = Listing::whereIn('id', $ids)->oldest('id')->first(); //using oldest()
A possible way to do this would be to sort the id column like so:
$listing = Listing::whereIn('id', $ids)->orderBy('id')->first();
I think you should try this:
$ids=[16,18,20];
foreach($ids as $id){
$listing[] = Listing::where('id', $id)->first();
}
Hope this work for you!
I have the problems using the array input as a value in WHERE clause.
But don't want to use more than once in WHERE clause code.
In my case, this is what I want :
$cond = array('job_id' => $job_id_var, 'job_name' => $job_name_var);
//WHERE clause
$this->where($cond); //only using once WHERE clause code like this, array as input
//which means
WHERE job_id = '$job_id_var' AND job_name = '$job_name_var'
is it possible to do that in codeigniter?
Yes, the ->where() method can support that.
Since you do not want to cascade it:
$this->db->where('job_id', $job_id_var);
$this->db->where('job_name', $job_name_var);
->where() can handle array input as well:
$cond = array('job_id'=>$job_id_var, 'job_name'=>$job_name_var);
$this->db->where($cond); // here, only used once.
$query = $this->db->get('hello_table');
$result = $query->result_array();
return $result;
I use Laravel DB::select to return specific data from DB,
the problem is only one row return when use print_r($result)
$questions = DB::select('
select text
from question
where id in (?)
order by field(id,?)',
array($question_ids_list_str,$question_ids_list_str));
print_r($questions);
how to return the rest of results?
thanks,
You may try something like this:
$ids = array(1, 2, 3); // Pass the ids in whereIn clause
$questions = DB::table('question')
->whereIn('id', $ids)
->orderBy('id')
->get(array('text')); // Only get text field
Try this:
$questions = DB::select('
select text
from question
where id in (?)
order by field(id,?)',
array($question_ids_list_str,$question_ids_list_str));
foreach($questions as $question) {
echo $question;
}
How to count the total number of rows of table and pass it to the variable? For example, I have product table and there are 10 products in it. I want to count all the rows and get the result 10 and pass it to the variable $rowcount. How can I do it?
use find('count')
For example:
$total = $this->Article->find('count');
If you want to use a condition in your query, then pass the conditions array, here is a sample code:
$pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));
you also try this with condition
$rowcount = $this->Product->find('count', array('conditions' => array('Product.fieldName' => 'value')
u can do it with find('count') method
$this->set('rowcount',$this->Product->find('count'));
or simply use count() function if had already the products in a variable $products to avoid another Mysql query
$this->set('rowcount',count($products));
By Using this you can count data with passing conditions in where
$assignment = TableRegistry::get('Assignment');
$query = $assignment->find();
$query->select(['count' => $query->func()->count('id')]);
$query->where(['Assignment.user_id'=>$user_id]);
$assignment = $query->toArray();
$assignment_count = $assignment[0]->count;
return $assignment_count;
In CakePHP 3.x you can query like this:
$count = $this->Students->find()->count();
Or
TableRegistry::get('Students')->find()->count();
Here student is example model. Replace with your model name.
Thanks.