Welcome Guest, Not a member yet? Register   Sign In
Mysql -> getting results from two tables?
#7

[eluser]Johan André[/eluser]
I would do it like this:

users
id
username
...
...


invoices
id
user_id
...
...

Using an id in the invoices table to refer users is more correct. What if a user with many invoices changes his username? Then you would have to update the username-colum in all invoices.

Get this list of invoices for a user with:

Code:
$this->db->select('*');
$this->db->from('invoices')
$this->db->join('users', 'users.id = invoices.user_id', 'inner');
$this->db->where('users.id', $user_id);
$result = $this->db->get()->result_array();


And then in view:

Code:
foreach($result AS $item) :
...
endforeach;


Messages In This Thread
Mysql -> getting results from two tables? - by El Forum - 05-21-2009, 09:32 AM
Mysql -> getting results from two tables? - by El Forum - 05-21-2009, 09:42 AM
Mysql -> getting results from two tables? - by El Forum - 05-21-2009, 09:46 AM
Mysql -> getting results from two tables? - by El Forum - 05-21-2009, 09:54 AM
Mysql -> getting results from two tables? - by El Forum - 05-21-2009, 01:39 PM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 02:35 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 02:53 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 04:46 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 04:55 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 05:36 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 06:01 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 06:16 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 06:57 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 08:19 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 10:55 AM
Mysql -> getting results from two tables? - by El Forum - 05-22-2009, 03:19 PM
Mysql -> getting results from two tables? - by El Forum - 08-20-2009, 06:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB