将SQL查询转换为yii2?

时间:2016-06-29 08:34:58

标签: yii2 yii2-advanced-app yii2-basic-app yii2-user

到目前为止,我一直在yii2中使用SQL查询,该查询在本地运行良好,但只要我将其部署到服务器就显示

image.jpt

我已经尝试将其更改为yii查询,因为它是在

下面的正确实现
$query = $connection->createCommand("SELECT a.name_of_flight, a.time_of_flight, (a.no_of_passenger - b.cnt) as avail, a.no_of_passenger FROM flight_schedule a LEFT JOIN (SELECT flight_time, COUNT(id) AS cnt FROM book_eticket WHERE flight_date='$date' AND company_name = '$comp_name' GROUP BY flight_time) b ON a.id = b.flight_time")->queryAll();

$query = (new \yii\db\Query());

$query
    ->select('a.name_of_flight, a.time_of_flight, (a.no_of_passenger - b.cnt) as avail, a.no_of_passenger')
    ->from('flight_schedule a')
    ->leftJoin('flight_time', ('COUNT(id) AS cnt FROM book_eticket'))
    ->where(array('and', 'flight_date=2016-6-29', 'company_name = Team5'))
    ->groupBy(['flight_time b','ON a.id = b.flight_time']);

$command = $query->createCommand();
$query = $command->queryAll();

但是我收到了错误:

enter image description here

有人可以帮我找出问题吗?提前致谢

1 个答案:

答案 0 :(得分:1)

第一个屏幕说明了访问问题。第二 - 像 date team 这样的操作数不是字符串。那是错的。你能引用它们吗?