【问题标题】:Retrieving multiple values associated with a single record. MySQL PHP Codeigniter检索与单个记录关联的多个值。 MySQL PHP Codeigniter
【发布时间】:2014-02-18 02:50:23
【问题描述】:

我需要查询下表的帮助。

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));* 

在 user_id 上链接的两个表, 这里每个用户可以有多个phone_numbers。

我需要提取一组用户以及他们的电话号码。我正在尝试执行以下操作。

在模型中

$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');
} 

当我的第一个 $query 返回多个用户时,如何从模型返回并为每个用户显示多个电话号码??

提前致谢, 普里姆

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

    尝试使用连接:

    $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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多