dongshou9878 2016-06-13 08:23
浏览 31

如何将sql查询更改为yii2

How to change the following sql query to yii2.

SELECT 
    a.id, a.name_of_flight, a.time_of_flight, (no_of_passenger-b.cnt) as avail 
FROM 
    flight_schedule a 
LEFT JOIN 
    (SELECT flight_time, COUNT(id) AS cnt FROM book_eticket WHERE flight_date='2016-06-01' GROUP BY flight_time) b 
ON a.id = b.flight_time
  • 写回答

1条回答 默认 最新

  • dongmieqiao3152 2016-06-16 04:49
    关注

    I'm not sure but you can use this logic. For more detail pls visit official page.

    flight_schedule::find()
        ->select(['id', 'name_of_flight', ' time_of_flight'])
        ->distinct()
        ->joinWith('bookEticket')
        ->all();
    

    And add relation to flight_schedule model:

    public function getBookEticket()
    {
        $this->hasOne(book_eticket::className(), ['id' => 'flight_time']);
    }
    
    评论

报告相同问题?