codeigniter Mysql join查询结果

时间:2017-04-29 05:38:13

标签: mysql codeigniter join

我有三张桌子。 用户, 产品(user_id是外键), reviews_ratings(user_id& product_id是外键)

mysql查询是

public Test4(int tableSize) {
    if (tableSize <= 0)
        throw new IllegalArgumentException("Table Size must be positive");

    table = new Vector<LinkedList<HashPair<K, E>>>(tableSize);
    //Prepare the fast lookup table (at least that's what I think it could be called)
    for (int i = 0; i < tableSize; i++) {
        table.add(new LinkedList<HashPair<K, E>>());
    }
}

结果,

$pid = some id;
$this->db->select('*');
$this->db->from('reviews_ratings');
$this->db->join('users', 'users.id = reviews_ratings.user_id');
$this->db->join('products', 'products.id = reviews_ratings.product_id ');
$this->db->where('reviews_ratings.id >', $pid);
$this->db->order_by('reviews_ratings.id', 'DESC');
$this->db->limit(1);

Array ( [0] => stdClass Object ( [id] => 14 [title] => blaaa [review] => blaaaa [rate] => 4 soooo on)) 不是rev​​iew_ratings表的ID。 14是product_id。除了[id]=>14值之外,一切都很好。我想要[id]中的review_ratings id值。 请帮我纠正一下。

2 个答案:

答案 0 :(得分:0)

尝试使用group by ...

$pid = some id;
$this->db->select('*');
$this->db->from('reviews_ratings');
$this->db->join('users', 'users.id = reviews_ratings.user_id');
$this->db->join('products', 'products.id = reviews_ratings.product_id ');
$this->db->where('reviews_ratings.id >', $pid);
$this->db->order_by('reviews_ratings.id', 'DESC');
$this->db->group_by('reviews_ratings.id');
$this->db->limit(1);

答案 1 :(得分:0)

通过添加此行解决了问题

$this->db->select('products.*,users.*,reviews_ratings.*');

而不是

  $this->db->select('*');