S
S
semki0962016-03-19 09:17:39
MySQL
semki096, 2016-03-19 09:17:39

Query in the database - fetching and counting rows in one function?

I need to select the last 3 comments for each article. I do it like this.

public function get_stena_comments($id)
        {
                $limit = 3;
                $query = $this->db->get_where('stena_comments', array('parent_id' => $id), $limit);
                return $query->result_array();
        }

And besides, I need to count the total number of comments to the same post. I do so
public function get_count_comments($id)
        {
                $query = $this->db->get_where('stena_comments', array('parent_id' => $id));
                return $query->result_array();
        }

The question is - can this be done more concisely, for example, with one request, rather than writing 2 functions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IceJOKER, 2016-03-19
@semki096

Purely logically think how mysql will give you both 3 comments and the total number?
You can do without two functions, but without two requests - no ( no perversions )

A
Ainur Shakirov, 2016-03-19
@Fqyeh29

Can. Just request ALL comments sorted by id or date, then count their number and take the last 3. All.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question