Yii加入查询结果不符合预期

时间:2016-08-13 12:30:01

标签: yii inner-join

select users.*, user_address.*, user_kyc_details.* , user_nominee.*,  user_bank_details.* 
from users, user_address, user_kyc_details, user_nominee, user_bank_details
where user_address.user_id=users.id and user_kyc_details.user_id=users.id and user_nominee.user_id=users.id and  user_bank_details.user_id=users.id

如何直接在$ query-> createCommand();作为参数,我试过但它不起作用

上面的查询工作正常并给出了预期的结果但是当我使用Yii时使用如下给出的连接时它给出了奇怪的结果(即没有加入适当的id和user_id)

$ query = new Query;

    $query->select([
    'users.*',
    'user_address.*'
    'user_kyc_details.*',
    'user_nominee.*',
    'user_bank_details.*'
    ])
    ->from('users')
    ->join('INNER JOIN', 'user_address','user_address.user_id = users.id')
    ->join('INNER JOIN', 'user_kyc_details','users.id =user_kyc_details.user_id')
    ->join('INNER JOIN', 'user_nominee','users.id =user_nominee.user_id')
    ->join('INNER JOIN', 'user_bank_details','users.id =user_bank_details.user_id');

    $command = $query->createCommand();
    $data = $command->queryAll();

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

如果您已直接执行查询,请执行命令

$sql = "select 
            users.*
          , user_address.*
          , user_kyc_details.* 
          , user_nominee.*
          ,  user_bank_details.* 
         from users, user_address, user_kyc_details, user_nominee, user_bank_details
         where user_address.user_id=users.id 
            and user_kyc_details.user_id=users.id 
              and user_nominee.user_id=users.id 
                and  user_bank_details.user_id=users.id";


$command = Yii::$app->db->createCommand( $sql );
$command->queryAll();