mysql - 选择查询 Yii2 中的左连接 - 'single' 中的未知列 'on clause'

标签 mysql sql yii2 left-join

为什么这个查询不起作用?

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/

相关文章:

php - 我应该在 SQL 还是 JS 中为 d3 过滤我的数据

使用 USE mydb 的 mysql 游标;

mysql - 在 SQL 中创建表时出现语法错误

sql - 使用存储过程访问数据

PHP MYSQL "merging"变量

php - 显示具有不同扩展名的图像 Yii2

MYSQL 选择具有特定技能的用户

mysql从用户获取条件前两位数字的最大数量

php - yii2 如何防止 swiftmailer 将电子邮件发送到垃圾邮件文件夹

database - Yii2 在链关系的最后一个添加条件