I am trying execute the query below:
(new Query())->select("id, 'post' as type, name")->from("post")->all();
But occurs this exception:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ''post'' in 'field list'
The SQL being executed was: SELECT `id`, `'post'` AS `type`, `name` FROM `post`
1. in D:\fontes\php\yii2-basic\vendor\yiisoft\yii2\db\Schema.php at line 520
2. in D:\fontes\php\yii2-basic\vendor\yiisoft\yii2\db\Command.php – yii\db\Schema::convertException(PDOException, 'SELECT `id`, `'post'` AS `type`,...') at line 855
3. in D:\fontes\php\yii2-basic\vendor\yiisoft\yii2\db\Command.php – yii\db\Command::queryInternal('fetchAll', null) at line 350
4. in D:\fontes\php\yii2-basic\vendor\yiisoft\yii2\db\Query.php – yii\db\Command::queryAll() at line 204
5. in D:\fontes\php\yii2-basic\controllers\SiteController.php – yii\db\Query::all()
yes, because there is no column with quotes, remove them from select part of post
But this is the example in doc:
http://www.yiiframework.com/doc-2.0/guide-query-builder.html#union
just try it, example may be incorrect (i think it was copy - paste mistake), you also can try array as input for select to simplify parsing. There are also similar examples in same docs above as you may noted, that dont use quotes
I've tried, same error
can you show exception? is it same as above?
yes it's the same.
This way works:
(new Query())->select(['id', new Expression('NULL as type'), 'name'])->from("post")->all();
it's right?
Most helpful comment
yes it's the same.
This way works:
it's right?