E
E
Egor2016-02-15 17:43:23
MySQL
Egor, 2016-02-15 17:43:23

How to make a query with a text string (literal string) in yii2 query builder?

Need a query like this

SELECT CategoryID, CategoryName, 'Northwind Category' AS Note
FROM categories;

categories-literal-string-1.gif
Here it works:
$query = (new \yii\db\Query())
    ->select("CategoryID, CategoryName, CONCAT('Northwind Category', '' ) AS Note")
    ->from('categories');

What other options are there?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor, 2016-02-15
@want2know

1) Use the string concatenation function of your DBMS

$query = (new \yii\db\Query())
    ->select("CategoryID, CategoryName, CONCAT('Northwind Category', '' ) AS Note")
    ->from('categories');

2) Use Expression
$query = (new \yii\db\Query())
    ->select([
         'CategoryID', 
         'CategoryName', 
         'Note' => new  \yii\db\Expression("'Northwind Category'")])
    ->from('categories');

M
Maxim Timofeev, 2016-02-15
@webinar

$query= (new \yii\db\Query())
    ->from('categories')
    ->where(['Note' => 'Northwind Category'])
    ->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question