Hi Have written the code below which is a I think a simple select with a join.
$this->db->select('PAS_User.user_first_name, PAS_User.user_last_name, PAS_User.user_avatar_url, AMS_Notification.noti_entitiy_id, AMS_Notification.noti_entity_type, AMS_Notification.noti_updated FROM AMS_Notification INNER JOIN PAS_User ON AMS_Notification.noti_from_user_id = PAS_User.user_user_id');
$this->db->from('AMS_Notification');
$this->db->join('PAS_User', 'PAS_User ON AMS_Notification.noti_from_user_id = PAS_User.user_user_id');
$this->db->where('AMS_Notification.noti_to_user_id', $userid);
$this->db->order_by("noti_updated", "desc");
$query = $this->db->get('AMS_Notification');
if($query -> num_rows() == 1) {
return $query->result();
} else {
return false;
}
However when I call the page it shows this query with two from stamens, can any one tell me what I am doing wrong here?
SELECT `PAS_User`.`user_first_name`, `PAS_User`.`user_last_name`, `PAS_User`.`user_avatar_url`, `AMS_Notification`.`noti_entitiy_id`, `AMS_Notification`.`noti_entity_type`, `AMS_Notification`.`noti_updated` FROM AMS_Notification INNER JOIN PAS_User ON AMS_Notification.noti_from_user_id = PAS_User.user_user_id FROM (`AMS_Notification`, `AMS_Notification`) JOIN `PAS_User` ON `PAS_User` `ON` AMS_Notification.noti_from_user_id = PAS_User.user_user_id WHERE `AMS_Notification`.`noti_to_user_id` = '7' ORDER BY `noti_updated` desc
$this->db->select('PAS_User.user_first_name, PAS_User.user_last_name, PAS_User.user_avatar_url, AMS_Notification.noti_entitiy_id, AMS_Notification.noti_entity_type, AMS_Notification.noti_updated');
$this->db->from('AMS_Notification');
$this->db->join('PAS_User', 'AMS_Notification.noti_from_user_id = PAS_User.user_user_id');
$this->db->where('AMS_Notification.noti_to_user_id', $userid);
$this->db->order_by("noti_updated", "desc");
$query = $this->db->get();
if($query -> num_rows() == 1) {
return $query->result();
} else {
return false;
}
No need to specify table name and JOIN in select part
Related
I have an SQL Query like this
return $this->db->query("SELECT * FROM `rincian_permintaan`
JOIN `permintaan` ON `permintaan`.`id_permintaan` = `rincian_permintaan`.`id_permintaan`
JOIN `users` ON `permintaan`.`id_peminta` = `users`.`user_id`
JOIN `cabang` ON `cabang`.`id_cabang` = `users`.`id_cabang`
JOIN `barang` ON `barang`.`id_barang` = `rincian_permintaan`.`id_barang`
JOIN `po` ON `rincian_permintaan`.`id_po` = `po`.`id_po` WHERE `po`.`id_cabang` = '201' AND `users`.`id_cabang` != 201 AND
NOT EXISTS(SELECT * FROM airwaybill WHERE airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan)
ORDER BY rincian_permintaan.created_at DESC")->result_array();
I want to change the format to:
$this->db->select('*');
$this->db->from('tableName');
$this->db->join('...');
$this->db->where('...');
And this is what i've tried:
$this->db->select('*');
$this->db->from('rincian_permintaan');
$this->db->join('permintaan', 'permintaan.id_permintaan = rincian_permintaan.id_permintaan');
$this->db->join('users', 'permintaan.id_peminta = users.user_id');
$this->db->join('cabang', 'cabang.id_cabang = users.id_cabang');
$this->db->join('barang', 'barang.id_barang = rincian_permintaan.id_barang');
$this->db->join('po', 'rincian_permintaan.id_po = po.id_po');
$this->db->where('po.id_cabang', '201');
$this->db->where('users.id_cabang != 201');
$this->db->select('*');
$this->db->from('airwaybill');
$this->db->where('NOT EXISTS airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan', '', FALSE);
return $this->db->get('rincian_permintaan')->result_array();
Error:
All you need is to generate sql string for subquery without executing it and use it in the outer query and produce the result.
$this->db->select('*');
$this->db->from('airwaybill');
$this->db->where('airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan');
$sub_query = $this->db->get_compiled_select();
$this->db->select('*');
$this->db->from('rincian_permintaan');
$this->db->join('permintaan', 'permintaan.id_permintaan = rincian_permintaan.id_permintaan');
$this->db->join('users', 'permintaan.id_peminta = users.user_id');
$this->db->join('cabang', 'cabang.id_cabang = users.id_cabang');
$this->db->join('barang', 'barang.id_barang = rincian_permintaan.id_barang');
$this->db->join('po', 'rincian_permintaan.id_po = po.id_po');
$this->db->where('po.id_cabang', '201');
$this->db->where('users.id_cabang != 201 ');
$this->db->where('NOT EXISTS('.$sub_query.')');
$query = $this->db->get();
$result = $query->result_array();
return $result;
here is my code of model
$this->db->select('*');
$this->db->from('vf_training_district', 'vf_training_firm', 'complain_form');
$this->db->where('complain_form.InstituteId', 'vf_training_firm.FirmId');
$this->db->where('complain_form.DistrictId', 'vf_training_district.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
getting error of unknown column complain_form.InstituteId. each and every column is same as in db
Hope this helps you :
$this->db->select(*);
$this->db->from('complain_form cf');
$this->db->join('vf_training_firm vftf', 'vftf.FirmId = cf.InstituteId');
$this->db->join('vf_training_district vftd', 'vftd.DistrictId = cf.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
for more : https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
You need JOIN:
SELECT *
FROM complain_form
JOIN vf_training_firm ON complain_form.InstituteId = vf_training_firm.FirmId
JOIN vf_training_district ON complain_form.InstituteId = vf_training_district.DistrictId
using Query Builder class Codeigniter:
$row = $this->db->select(*)
->from('complain_form')
->join('vf_training_firm', 'complain_form.InstituteId = vf_training_firm.FirmId')
->join('vf_training_district', complain_form.InstituteId = vf_training_district.DistrictId)
->get();
return $row->result_array();
How to get count of the sql search query
$u = new user();
$sql = "SELECT a.id FROM `accounts` AS a LEFT JOIN `users` AS u ON a.id = u.id WHERE a.id IS NOT NULL ";
$gender = $this->input->post('gender');
if($gender != NULL)
{
$sql .= "AND u.gender = '".$gender."' ";
}
$u->query($sql);
How to get count of the query results in $u->query($sql); .I need to set a validation on it. If the query results count is 0 , i need to set a message. Im using PHP Codeigniter ,datamapper library.
Thank you!!!
Just using count() function like this
...
$result = $u->query($sql);
$total_data = count($result);
if($u->exists())
{
echo "Found" // Do something
}
else
{
echo "Nothing found" //Do something
}
$result = $this->db->get();
For Codeigniter use $count = $result->num_rows(); // HERE IS YOUR COUNT
For OOP PHP use $count = $result->num_rows;
Just want to get user_id and review_text values from table reviews, select name and city from table users where id=user_id and then return review_text, username and city to controller. Please help me.
public function did_get_reviews($item_id){
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$user_id = 'user_id' //??
$this->db->select('name','city');
$this->db->from('users');
$this->db->where('id', $user_id);
$query = $this->db->get('review_text','username','city'); //??
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
Update 1:
I just tried to join tables but it returns the values for review_text only but I also want to get values for name and city. Please check the code below:
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text','users.name','users.city');
$this->db->from('reviews','users');
$this->db->where('reviews.post_id', $item_id);
$this->db->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
i think you need to use join query in SQL. if you use this code you can access to what you want
$result = $this->db->select('review, username, city')
->from('reviews')
->join('city', 'city.user_id = review.user_id')
->get();
print_r($result);
for more information about how you can write join query(left, inner or right) in codeigniter you can see this link
i hope that this code solve your problem
UPDATE
in your new question. your code have a little buggy. you need to write your select in this way
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text,users.name,users.city')
->from('reviews','users')
->where('reviews.post_id', $item_id)
->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
in codeigniter select Active record; any column name separate with each other by , only (not by ' and ,)
in codeigniter documentation wrote that
Permits you to write the SELECT portion of your query:
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
// Produces: SELECT title, content, date FROM mytable
You can get the value for user_id using:
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$result = $this->db->get();
print_r($result);
use the same $this->db->get(); to get the values of the second query as well. Once you get the values inside a variable you can iterate it. Something like:
foreach ($result->result() as $row)
{
echo $row->name;
echo $row->city;
}
im using active record, its all working ok but i want to set the $data["totalres"] to the total results, i mean, the same query but without the LIMIT
the problem is the previous statements gets unset when you do a query modifier, so i cant even add the $this->db->limit() after i get the results.
any ideas? i think its a bad practice to 'duplicate' the query just to do this
function get_search($start, $numrows, $filter = array())
{
...
$this->db
->select("emp")
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start);
...
$q = $this->db->get();
// number of rows WITHOUT the LIMIT X,Y filter
$data["totalres"] = ???????;
if ($q->num_rows() > 0)
{
$data["results"] = $q->result();
} else {
$data["results"] = array();
}
return $data;
}
You can use SQL_CALC_FOUND_ROWS to get the number of rows that would have been returned sans-LIMIT. Note the ,FALSE in the select line. This tells CodeIgniter not to try to escape the SELECT clause with backticks (because SQL_CALC_FOUND_ROWS is not a field, and CodeIgniter doesn't realize that).
$this->db
->select("SQL_CALC_FOUND_ROWS emp", FALSE)
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start);
$q = $this->db->get();
Then after that query is ran, we need run another query to get the total number of rows.
$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
$data["totalres"] = $query->row()->Count;
Try this:
function get_search($start, $numrows, $filter = array()){
$tmp= $this->db
->select("emp")
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->_compile_select();
$q= $this->db->limit($numrows, $start)->get();
// number of rows WITHOUT the LIMIT X,Y filter
$data["totalres"] = $this->db->query($tmp)->num_rows();
if ($q->num_rows() > 0){
$data["results"] = $q->result();
} else {
$data["results"] = array();
}
return $data;
}
I would actually suggest the use of CIs query caching.
To use this, you start the cache, build the query without the limits in place. Run your query to get the full count, then apply the limit and run the query to get your list for your page with the offset you need.
The cache will remember the variables that have been defined.
You can then clear the cache for subsequent queries.
Example
$this->db->start_cache();
// Where, like, having clauses in here
$this->db->stop_cache();
$count = $this->db->get('table')->num_rows();
$this->db->limit('limit', 'offset');
$result = $this->db->get('table')->result_array();
$this->db->flush_cache();
$this->db
->select("SQL_CALC_FOUND_ROWS emp", FALSE)
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start); $q = $this->db->get();
$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
$data["totalres"] = $this->db->get()->row()->Count;
CodeIgniter 2.1.0 not run, below code will fix it.
$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
$objCount = $query->result_array();
$data["totalres"] = $objCount[0]['Count'];