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);
Related
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
I'm working with CodeIgniter and Active Record (mysql database), to write a special query. Here is the working query :
$this->db->select('moo_aauth_users.*', false);
$this->db->select('moo_tasks.id AS task_id, moo_tasks.user_id, moo_tasks.task_status, moo_tasks.task_type, moo_tasks.skill_category, moo_tasks.tasker_hired, moo_tasks.task_city_id, moo_tasks.completed, moo_tasks.hours_spend, moo_tasks.additional_fees, moo_tasks.total_price, moo_tasks.paid, moo_tasks.tasker_asked_id, moo_tasks.tasker_asked_response, moo_tasks.tasker_asked_rate, moo_tasks.title, moo_tasks.description, moo_tasks.expiration_date, moo_tasks.telephone, moo_tasks.zipcode, moo_tasks.address, moo_tasks.created_stamp, moo_tasks.updated_stamp, moo_tasks.assigned_stamp, moo_tasks.completed_stamp, moo_tasks.provider, moo_tasks.country, moo_tasks.task_zone', false);
$this->db->from('moo_tasks');
$this->db->join('moo_aauth_users', 'moo_aauth_users.id = moo_tasks.user_id');
$this->db->where('moo_tasks.task_status', TASK_STATUS_PENDING);
But now, I would like to add a special where clause based on another table.
I have a table moo_offers containing different offers corresponding to a task. In my query, I want to check if the task I'm retrieving DOES NOT contains a corresponding offer in the moo_offers table.
Example :
moo_tasks: id 1 / ...
moo_tasks: id 2 / ...
moo_offers: task_id 1 / ..
With the query, only moo_tasks.id = 2 will be retrieved, not the id 1 because it has an offer in the second table.
I tried multiple thinks in my query, but did'nt works :
$this->db->join('moo_offers', 'moo_offers.id_task = moo_tasks.id');
$this->db->where('moo_offers.id_tasker NOT IN (SELECT `id_tasker` FROM `moo_offers` WHERE id_task = moo_offers.id_task && id_tasker = '.$filter['tasker_id'].')', NULL, FALSE);
Have you and idea please? Thanks!
$this->db->select('*');
$this->db->from('tbl_user');
$this->db->join('tbl_userinfo', 'tbl_user.user_id = tbl_userinfo.user_id');
$this->db->where('tbl_user.user_id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
this is what i did and it worked.
can you help I have two tables images and note. I would like to get all items from these tables and order by date. It is possible I am using active record in codeigniter. Tables are independent.
Thank you for replies.
this should work:
$this->db->orderby("date", "ASC"); //or desc
$query = $this->db->get('images'); //or $query = $this->db->get('note');
$result=$query->result_array();
print_r($result);
or if you want use union all
$this->db->query('SELECT * FROM (SELECT id, date FROM images UNION ALL SELECT id, date FROM note) result ORDER BY result.date');
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order from http://www.w3schools.com/sql/sql_union.asp.
I think you want a Cross Join.
Here is your codeigniter code
$this->db->from("images ,note");
$this->db->select("images.*,note.*);
//make sure both dont have same column name other wise use this
//$this->db->select("images.column1,images.column2,note.column1);
$this->db->orderby("images.date", "DESC");
//you can add more orderby
//you can add where condition too
$result=$this->db->get()->result_array();
Now you will get the cross product.
Hope it will help you.
In which table do you have date column? I assume you have it in images table.
$this->db->select('images.coulmn1, images.culmn2, images.date_col, note.col1, note.col2');
$this->db->from('images');
$this->db->join('note', 'note.key1 = images.key1); //key1 is key field to relate both table.
$this->db->order_by("images.date_col", "desc");
$result = $this->db->get()->result_array();
Hope it will help.
Try this:
$this->db->select('*');
$this->db->from('images');
$this->db->join('note');
$this->db->orderby("date column name", "ASC");
$query = $this->db->get();
$result=$query->result_array();
print_r($result);
i am using codeigniters sql selection to select users where not in a set of id's.
$this->db->select('fbuid')->where_in('fbuid', $friends);
$query = $this->db->get('users');
im am trying to add another clause where it check for and not in friends table (id column). im trying to add use
$this->db-where_not_in
but i can't seem to get it to work
heres a pseudocode sql statement
SELECT fbuid FROM users WHERE IN () BUT NOT IN the friends TABLE ID COLUMN
Try this:
$query = $this->db->select('fbuid')
->from('users')
->where_in('fbuid', $friends)
->where_not_in('fbuid', $otherFriends);
EDIT:
You can either make a subquery before and store it in array OR do this in one step:
$query = $this->db->select('fbuid')
->from('users')
->where_in('fbuid', $friends)
->where('`fbuid` NOT IN (SELECT `uid` FROM `another_table`)', NULL, FALSE);
NULL, FALSE are important.
I need a query that looks like:
select * from table where id=5 and (eid = 1 or eid = 2 or eid = 10)
and I have tried
$this->db->select()->from(MEMB_EVENTS);
$this->db->where('mID', $mID);
with various means of where or_where get_where and no luck forming a query similar to the above need was hoping someone could shed some light for me on the subject
I think You can do that by using something like
$this->db->select()->from(MEMB_EVENTS);
$this->db->where('mID', $mID);
$this->db->where("(eid='1' OR eid='2' OR eid='10')", NULL, FALSE);