Get rows where one column is unique and another column is the lowest value relative to the unique column

Timothy Coetzee :

I am struggling with a relatively simple problem but just cant get my head around it.

I have a method getNextRound() which returns an array of numbers. The numbers represents the week numbers in the db table.

I then have a second method getUpcomingGames() where I call the the first method I then want to use the numbers from the first method to use in my query.

Here is an example: METHOD 1

public function getNextRound(){

        $sql = "SELECT min(weekNum) from schedule WHERE schedule.gameDateTime > NOW() GROUP BY tournament ORDER BY gameDateTime ASC";
        $stmnt = $this->db->query($sql);
        if ($stmnt->num_rows() > 0) {
            print_r($stmnt->result());
            return $stmnt->result();
        }
        return false;
    }

RESULT FROM ABOVE METHOD / QUERY

array (size=3)
  0 => 
    object(stdClass)[24]
      public 'min(weekNum)' => string '38' (length=2)
  1 => 
    object(stdClass)[25]
      public 'min(weekNum)' => string '14' (length=2)
  2 => 
    object(stdClass)[26]
      public 'min(weekNum)' => string '7' (length=1)

I now want to use the data in the array to get all info contained in schedule table related to the week number.

My problem is here

METHOD 2

public function getUpcomingGames()
    {
//HERE I WANT TO GET ALL INFO FROM SCHEDULE WHERE ROUND = $week
        $rounds[] = $this->getNextRound();
        foreach ($rounds as $round) {
            $sql = "SELECT *  from  schedule WHERE weekNum = '$round' ORDER BY gameDateTime ASC ";
            $data[] = $this->db->query($sql);
            var_dump($data);
        }

ERROR: Among others I get an array to string conversion error.

I have looked through the codeigniter docs, but could not find the method which I am looking for.

DB TABLE qu

QUESTION:

  • Is there a query method in CI where I can insert an array into a query and loop over array (), if that makes sense?

  • How can I improve / fix the above query?

mickmackusa :

I presume you require a query like this:

SELECT *
FROM schedule AS parent
JOIN (
    SELECT tournament,
           MIN(weekNum) AS nextWeek
    FROM schedule AS child
    WHERE gameDateTime > NOW()
    GROUP BY tournament
) ON parent.tournament = child.tournament AND parent.weekNum = child.nextWeek
ORDER BY gameDateTime";

This will maintain the relationship between tournaments and weekNums when passing qualifying rows to the parent query. This way the result set remains true even if you have a non-qualifying tournament with a qualifying WeekNum.

The codeigniter equivalent is:

$this->db->select('tournament, MIN(weekNum) AS nextWeek');
$this->db->from('schedule');
$this->db->where('gameDateTime >', 'NOW()', false);
$this->db->group_by('tournament');
$subquery = $this->db->get_compiled_select();


// $this->db->select('*'); <- not necessary
$this->db->from('schedule AS parent');
$this->db->join('(' . $subquery . ') AS child', 'parent.tournament = child.tournament AND parent.weekNum = child.nextWeek');
$this->db->order_by('gameDateTime');
return $this->db->get()->result();

*Disclaimer: None of the above is tested. Confirmation of success would be appreciated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169358&siteId=1