简体   繁体   中英

Retrieving multiple values associated with a single record. MySQL PHP Codeigniter

I need help with a query on following tables.

table user(
    user_id char(5),
    username char(12),
    columnx char(10));*

table phone(
    user_id char(5),
    phone_number  number(10),
    primary(Y, N) char(1));* 

Both tables linked on user_id, Here each user can have multiple phone_numbers.

I need to pullout a set of users and along with their phone numbers. I am trying to do the following.

in model

$this->db->where('columnx', 'something');
$query = $this->db->get('users');
foreach($query->result() as $row) {
    $this->db->select('phone_number');
    $this->db->where('user_id', $row->user_id);
    $this->db->where('primary', 'Y');

    $q = $this->db->get('phone');
} 

How do I return from model and display multiple phone number for each user, when I my first $query returns multiple users??

Thanks in advance, Prim

Try using join:

$query = $this->db->select('*')
                ->from('user as a')
                ->join('phone as b', 'a.user_id = a.user_id')
                ->where(array('a.columnx' => 'something'));
                ->get();
var_dump($query->result());

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