Active record - 3 results found, but only one bein

2019-03-04 11:30发布

问题:

The query below is returning that 3 results are available, but it is only returning one entry id.

How can I have the three entry_id's returned?

$this->EE->db->select('entry_id, count(portfolio_number) AS results');
$this->EE->db->from('submissions');
$this->EE->db->where('type_id', '1'); 
$this->EE->db->where('member_group', $member_group); 
$this->EE->db->group_by('portfolio_number'); 
$this->EE->db->having('results = 3'); 
$query = $this->EE->db->get();
$submissions = $query->result_array();

print_r($submissions);

EDIT:

I have a table columns entry_id, member_group, type_id and portfolio_number.

The portfolio_number column will have a number between 1 and 7.

I need to query the database for 3 rows that have the same portfolio_number (as well as matching type and member_id) and return the entry_id for each of those three rows.

There must be 3 results, else I don't want to show them.

回答1:

You can handle it like this

$results    =   $this->EE->db
                        ->select('entry_id')
                        ->from('submissions')
                        ->where('type_id', '1')
                        ->where('member_group', $member_group)
                        ->group_by('portfolio_number')
                        ->get()
                        ->result_array();

if(count($results)){
    return $results
}else {
    return false;
}


回答2:

Your $this->EE->db->group_by('portfolio_number'); is causing a single row with aggregated data to be returned.

If you want all the ids to be returned as well, you can try adding

$this->EE->db->select('GROUP_CONCAT(entry_id) AS entry_ids', false);

and then splitting the entry_ids field in PHP:

str_getcsv($submissions);

Edit: I put in the second argument for the select query to prevent backticks being placed around the custom select query.



回答3:

Can you please replace this code

$this->EE->db->group_by('portfolio_number'); //It will return only one row per portfolio_number
$this->EE->db->group_by('portfolio_number,entry_id'); // It will return one row per portfolio_number and entry_id

I think it will return 3 rows of different entry_id