How to change the following query with the help of query builder codeigniter?
$query=$this->db->query("select a.*,b.nama from transaksi a,
anggota b
where a.no_transaksi='$nomor' and a.no_transaksi
not in(select no_transaksi from pengembalian)
and a.nomor_anggota=b.nomor_anggota");
Note: just want to know another way
Try this:
$this->db->select('a.*, b.nama');
$this->db->from('transaksi a, anggota b');
$this->db->where('a.no_transaksi', $nomor);
$this->db->where('`a.no_transaksi` NOT IN (SELECT `no_transaksi` FROM `pengembalian`)', NULL, FALSE);
$this->db->where('a.nomor_anggota = b.nomor_anggota');
$result = $this->db->get()->result();
The ,NULL,FALSE in the where() tells CodeIgniter not to escape the query, which may mess it up.
Source: subquery in codeigniter active record
Related
Is there away to cross join from php. In example:
Currently I query a database like so:
$company_id = 20;
$templates_data = $this->db->select('template_id')
->from('dr_template_relational')
->where('dr_template_relational.company_id',$company_id)
->get()
->result_array();
What I'm looking to do is something like this:
->from('dr_template_relational')
->cross_join()
There's several responses to this question on SO but post reference a regular sql query like so:
"SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";
This would be the way to do it in SQL query but the point here is to do it in php and get the data returned with a cross join. I also realize the query can be made with in php to have it select the data and join it with code but my question is related to is there away to simply add ->cross_join() or something like that.
You can run raw query in codeigniter to solve your problem as below:
$sql 'your query here with cross join';
$query = $this->db->query($sql);
return $query->result_array();
Hope it helps you :)
You can use CI join method.
$company_id = 20;
$templates_data = $this->db->select('dr_template_relational.template_id')
->where('dr_template_relational.company_id',$company_id)
->join('table','dr_template_relational.company_id=table.company_id','LEFT')
->get()
->result_array();
where 'LEFT' is the join type
I have join table with where in
I am wondering if there is a way i can say something like this :
where kd_x in (select kd_x from master_code where a='11921212')
I'm trying to do this with active record like this but is doesn't give me any data
$this->db->where_in('kd_x',array('select kd_x from MT_master where a="11921212"'));
Help me Please, Thank You
->where() you can use 2nd and 3rd argument, so that any string you can pass
$this->db->where('`kd_x` IN (select `kd_x` from `MT_master` where a="11921212")', NULL, FALSE);
OR
//Create where clause
$this->db->select('kd_x')
->where('a','11921212')
->from('MT_master');
$where_clause = $this->db->get_compiled_select();
//Create main query
$this->db->select('*');
->from('your_table');
->where("`kd_x` IN ($where_clause)", NULL, FALSE);
is it possible to pass the variable value in mysql query in codeigniter ?
I want to select records from database that have id same as my session id.
my code is :
$this->db->select('*'); // <-- There is never any reason to write this line!
$this->db->from('products');
$this->db->where('id_admin = (SELECT id_admin FROM users WHERE id='.$this->session->userdata('user_id').')', NULL, TRUE);
can we concatenate variable values in database queries?
This code not given any error, but not give any result also.
Use join in active records
$this->db->select('p.*');
$this->db->from('products as p');
$this->db->join('users','p.id_admin=users.id');
$this->db->where('users.id',$this->session->userdata('user_id'),false);
$query = $this->db->get();
An Example Reference of how to write the JOIN Query in CI as per the Documentation of the Active Records.
$article_id = $this->input->post('article_id');
$this->db->select('*');
$this->db->from('articles');
$this->db->join('news', 'news.id = articles.id');
$this->db->where('articles.id',$article_id);
$query = $this->db->get();
The above code will produce the query as follows for execution.
SELECT * FROM articles JOIN news ON news.id = articles.id WHERE articles.id='10'
Provided the passed id is 10
In order to view the Result of the executed query you must perform the below code.
print_r($query->result());
Try like this..use joining of two tables.
<?php
$this->db->select('products.*');
$this->db->from('products');
$this->db->join('users','products.id_admin=users.id_admin');
$this->db->where('users.id',$this->session->userdata('user_id'));
$query = $this->db->get();
print_r($query->result_array());//your array of records
In my controller :
I have function like this :
$this->admindata->examview($a,3);
In model, I just have function like this :
function examview($examid, $examtipe){
$this->db->select("exam_id");
$this->db->from("mainexam");
$query = $this->db->get()
return $query->result();
}
And i got error :
Column 'id_group' in field list is ambiguous
SELECT `mu`.`obli`, `mu`.`id_exam_question`, `p`.`id_question`, `question`, `type_question`, `m`.`id_gabungan`, `p_parent`, `id_group` FROM (`exam`, `exam` mu) LEFT JOIN `randomexam` c ON `mu`.`id_group`= `c`.`id_question_order` LEFT JOIN `question` p ON `p`.`id_question` = `c`.`id_question` LEFT JOIN `main` m ON `m`.`id_question` = `p`.`id_question` WHERE `mu`.`id_exam` = '10' GROUP BY `mu`.`id_exam_question` ORDER BY `question_type` asc, LIMIT 0
I don't even have JOIN in my function. And If I delete $this->admindata->examview($a,3), My error has gone. Codeigniter try to call other function I think.
Nah it's tripping up on something else, a query before this one is in need of fixing. Your query here would be like
SELECT exam_id FROM mainexam;
PHP & MYSQL: How to resolve ambiguous column names in JOIN operation?
Try to track down the query causing the error and apply bandages.
In my application i have three tables, reservation, patient and sub_unit, i need to take the patient_id from reservation table and query the patient table for patient data,same time i need to take the sub_unit_id from the reservation table and query the sub_unit name from the sub_unit table... i need to put all this data in to an one array in the sequence like
patient_id, sub_unit_name, patient_name, address and pass it to the Codeigniter table class to draw a table.
How can I query three tables in the same time to query out this data? can you guys help me out?
Using code igniter syntax it can be done as follows -
$this->db->select('r.patient_id, s.sub_unit_name, p.patient_name, p.address');
$this->db->from('reservation r');
$this->db->join('patient p', 'p.id = r.patient_id');
$this->db->join('sub_unit s', 's.id = r.sub_unit_id');
$query = $this->db->get();
You can check your formed query by -
echo $this->db->_compile_select();exit;
Select r.patient_id, s.sub_unit_name, p.patient_name, p.address
from reservation r, sub_unit s, patient p
where r.patient_id = p.patient_id and r.sub_unit_id = s.sub_unit_id
The join syntax is very straightforward in SQL. You are probably looking for something like this:
SELECT reservation.patient_id,
sub_unit.sub_unit_name,
patient.patient_name,
patient.address
FROM reservation
JOIN patient ON (patient.id = reservation.patient_id)
JOIN sub_unit ON (sub_unit.id = reservation.sub_unit_id);
In MySQL, the default join is an Inner Join, which I think is what you're looking for. You may also want to look into Outer Joins which are also very useful.
it worked guys , i did it like this using Codeigniter active records ,hope you guys can use it too
function get_data(){
$sql = 'SELECT * FROM visit,patient,sub_unit WHERE visit.patient_patient_id = patient.patient_id AND visit.sub_unit_sub_unit_id = sub_unit.sub_unit_id';
$this->db->order_by("reference_number", "desc");
$query = $this->db->query($sql);
return $query;
}
thanx for all your support!