A
A
Alexander Verbitsky2018-07-04 07:32:45
Yii
Alexander Verbitsky, 2018-07-04 07:32:45

How to build a database query?

For Yii's, the question is actually simple =)
There is a query to the database (the example was taken from the page )
which from one table, selects the values ​​\u200b\u200bof which are not in another table:

SELECT * FROM artist 
LEFT JOIN painting
ON artist.a_id = painting.a_id
WHERE painting.a_id IS NULL;

How to build this query using yii2 methods??

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
enchikiben, 2018-07-04
@VerbAlexVlad

$rows = (new \yii\db\Query())
    ->select(['*'])
    ->from('artist')
    ->leftJoin('painting', 'artist.a_id = painting.a_id')
    ->where(['painting.a_id' => null])
    ->all();

something like this, not tested but it should work

D
Dmitry Kim, 2018-07-04
@kimono

If there are corresponding models Artist and Painting, then you can do this:

Artist::find()->leftJoin('painting', 'artist.a_id = painting.a_id')->where(['painting.a_id' => null])->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question