简体   繁体   中英

SQL active record query join with where empty clause

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.


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM