Welcome Guest, Not a member yet? Register   Sign In
Sub select active record
#1

[eluser]bradleyg[/eluser]
Hi!

I've got two tables. "users" and "listings".

In the users table the fields are "id", "name", "email", "password".
In the listings table the fields are "mtype", "userid".

I'm trying to do a sub select statement to get the email from the users table using the userid from the listings table. I'm not sure what I need to do but I think it may involve joining a table.

I would think something like this:
SELECT email FROM users WHERE (SELECT id FROM listings WHERE userid = I CANT WORK IT OUT

This is what I have already. Maybe I am going the wrong way about this?

Code:
function get_active_listings(){
    $this->db->select('id, mtype, userid'); <-this is where I want to swap the userid for email
    $this->db->order_by('id', 'desc');
    $this->db->where('running', '1');
    $query = $this->db->get('listings');
    return $query->result_array();
}

Thanks for your help.
#2

[eluser]Adi Setiawan[/eluser]
perhaps
Code:
$this->db->select('id, mtype, userid');
$this->db->from('users');
$this->db->join('listings', 'listings.userid= users.id');




Theme © iAndrew 2016 - Forum software by © MyBB