为什么这个查询不起作用?
public function actionInsert()
{
$model = new NotificationsEvents();
$date_at = (new Query())
->select(['single-events.date_at'])
->from('single-events')
->leftJoin('user', 'user.birthday = single-events.date_at');
$event_id = (new Query())
->select(['single-events.id'])
->from('single-events')
->leftJoin('user', 'user.id = single-events.id');
(new Query())->createCommand()->insert('notifications_events', [
'type' => 7,
'date_at' => $date_at,
'event_id' => $event_id,
])->execute();
}
我需要在 notifications_events.date_at
中插入用户生日,在 notifications_events.event_id
中插入用户 ID,但此代码不起作用:
Unknown column 'single' in 'on clause'
最佳答案
single-events
不是表的有效/安全名称,因为它包含 -
。您应该在每个可能的地方引用它:
public function actionInsert() {
$model = new NotificationsEvents();
$date_at = (new Query())
->select(['{{single-events}}.date_at'])
->from('single-events')
->leftJoin('user', 'user.birthday = {{single-events}}.date_at');
$event_id = (new Query())
->select(['{{single-events}}.id'])
->from('single-events')
->leftJoin('user', 'user.id = {{single-events}}.id');
(new Query())->createCommand()->insert('notifications_events', [
'type' => 7,
'date_at' => $date_at,
'event_id' => $event_id,
])->execute();
}
或者为表使用一些更实用的名称,例如single_events
。
关于mysql - 选择查询 Yii2 中的左连接 - 'single' 中的未知列 'on clause',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56722879/