【问题标题】:Retrieve only one row of comment仅检索一行评论
【发布时间】:2013-06-25 16:05:22
【问题描述】:

我有一个用 codeigniter 开发的网站,我想在其中检索特定 tee 的评论。 我有一个这样的 T 恤:

- id
- user_id
- name
- created
- modified

还有像这样的 tee_cmets 表:

- id
- user_id
- tee_id
- created
- modified

我已经完成了这个查询:

 $this->db->select('*,tee.id as id,tee.created as created, tee_comments.id as tee_comments_id, tee_comments.created as tee_comments_created, tee_comments.modified as tee_comments_modified');
    $this->db->from('tee');
    $this->db->join('tee_comments', 'tee_comments.tee_id = tee.id','left outer');
    $this->db->order_by("tee.created", "desc");
    $query = $this->db->get();

通过此查询,我检索了两行 tee,因为该 tee 中有两个 cmets。

我的目标是只检索一行,其中有一组评论,例如:

tee{
  id,
  name,
  created,
  modified
  comment{
     [0]
         id,
         tee_id,
         comment,
         created,
         modified
     [1]
         id,
         tee_id,
         comment,
         created,
         modified
  }
}

我已尝试加入: - 剩下 - 对 - 左外 - 右外

但不能解决问题,有没有办法做到这一点? 谢谢

【问题讨论】:

  • 数据库查询从表(组合)中检索行。如果你想要一个嵌套结构,你必须自己在 php 中生成它。
  • 您必须手动将评论数据与相应的 tee 记录汇总
  • 您的问题表明您只想要 1 行评论,但您想要实现的目标包括多个 cmets?
  • @jeroen 他正在使用 CI,它返回一个“嵌套结构”
  • @SpYk3HH 我们可能使用了不同的nested structures定义;它返回包含信息的行,但这些行不包含 OP 需要的信息子行(cmets ...)。

标签: php sql codeigniter


【解决方案1】:

我喜欢 CodeIgniter!我经常使用它!这里有 2 个非常简单的选项:

一种方法是使用limit。

$this->db->select('*,tee.id as id,tee.created as created, tee_comments.id as tee_comments_id, tee_comments.created as tee_comments_created, tee_comments.modified as tee_comments_modified');
$this->db->join('tee_comments', 'tee_comments.tee_id = tee.id','left outer');
$this->db->order_by("tee.created", "desc");
$query = $this->db->limit(1)->get('tee');

另一种方法是获取results Array中的第一项

$query = $this->db->get();
$results = $query->result(); // gets return as an array
$row = $results[0]; // will be first row in results array

请记住,$row 将以 object(stdClass) 的形式返回,这意味着您必须从中检索内容,例如 $row->column_name。

下面是我喜欢在通话后使用的一个方便的小 sn-p。它使行 Object's Array's 代替。

$results = $db->get('tee')->result(); // snippet comes after you have result array
// snippet here
foreach ($r as $k => $v) { $results[$k] = array(); foreach ($v as $kk => $vv) { $results[$k][$kk] = $vv != "NULL" ? trim($vv) : ""; } }

【讨论】:

    【解决方案2】:

    使用$this->db->limit(1) 检索单个记录。

    根据this question上接受的回答,你可能需要在select前加上limit语句:

    $this->db->limit(1);
    $this->db->select('*,tee.id as id,tee.created as created, tee_comments.id as tee_comments_id, tee_comments.created as tee_comments_created, tee_comments.modified as tee_comments_modified');
    $this->db->from('tee');
    

    【讨论】:

    • 但我必须用他们的评论检索所有 tee,例如我知道 limit 只能检索一条记录,但不是这种情况
    • 嗯,在那种情况下,它可能是不可能的。在这里查看答案:stackoverflow.com/questions/6105451/…
    【解决方案3】:

    选项一:

    for ($i = 0; $i < count($records); $i++)
    {
     $data = $records[$i];
     // work with date.
     if($i == 1)
       break;
    }
    

    选项二: 只需将第一行分配给 var。

    $row = $records['tee']['comment'][0];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      相关资源
      最近更新 更多