There are 2 tables:

field -id -title field_val -id -field_id -user_id 

The task: to make a selection of all fields of the field table with a connected table field_val , the type of connection OUTER .

But to set the external table field_val additional condition field_val.user_id = x . And, if the field with the condition field_val.user_id = x does not exist, then output NULL , keeping the field from the first table.

The problem is that if there is no user_id field with a value of Х in the field_val table, then the field of the field table is also not displayed.

I will not give an example of what I did, but it does not work, because I make up a request for AR in YII.1

    1 answer 1

    You can use LEFT OUTER JOIN

     SELECT f.*, v.* FROM field AS f LEFT OUTER JOIN field_val AS v ON f.id = v.field_id AND v.user_id = x 
    • Thanks It works. But the criteria in yii did not succeed, the DAO had to be used - Roman Maltsev