在Yii中写入查询[重复]

This question already has an answer here:

I have the following query...and want to add few joint additional joint statements in the existing query...posting plane sql query so that it give u idea what I am actually looking for .

/* simple sql query */
SELECT * 
FROM job_profile
INNER JOIN user ON user.id = job_profile.user_id
INNER JOIN category ON category.id = job_profile.category_id
WHERE category_id =1
AND experience =2
AND key_skills LIKE  '%php%'
LIMIT 0 , 30 
/* simple sql query */

I have tried the follwing in yii.And its working fine till now...but I dont know how to add joint statement.

  $results = SearchEmployee::model()->findAll("category_id=:category AND key_skills like :skill AND experience=:experience", array(
        'category'=>$category,
        'skill'=>'%'.$skills.'%',
        'experience'=>$experience
        ));

I just want to keep the above statement and just need to add above mentioned joint query in the statement.And I dont want to use adbCriteria for time beign.

</div>
doutu1939
doutu1939 你为什么要重新发布完全相同的问题?stackoverflow.com/questions/23197660/...
6 年多之前 回复

2个回答

Good Question . You can use With() by the following way

Parent::model()->with(array(
  'childs',
  'childs.grandchilds' => array(
    'select' => false,
    'condition' => 'grandchilds.gender = "MALE"',
  ),
)->findAll(...); 

More Eg:

$users=User::model()->with(array(
    'posts'=>array(
        // we don't want to select posts
        'select'=>false,
        // but want to get only users with published posts
        'joinType'=>'INNER JOIN',
        'condition'=>'posts.published=1',
    ),
))->findAll();

I think you could do this one with relations

the job_profile table (I do not know which model you use it in) would need to have 2 relations to user and to category

Then it would automatically make the join for you, and you would only need to search on the model itself.

Otherwise, if you want to do a complex Select statement, though this is not much MVC like, you can use the query builder

or try it simply this way:

$result = Yii::app()->db->createCommand('YOUR SELECT STATEMENT')->queryAll();

donmqryh49993
donmqryh49993 好的......解决了......谢谢
6 年多之前 回复
douweng3383
douweng3383 它现在在我的视图中显示错误'试图获取非对象的属性'到现在为止它不存在
6 年多之前 回复
dqfxao2898
dqfxao2898 ok Asped ...我刚刚更正了我的查询....如果你可以节省一些时间,你可以和我一起聊天吗... chat.stackoverflow.com/rooms/50940/yii-framework
6 年多之前 回复
douxianji6181
douxianji6181 所以这只是意味着你的选择中有一个错误,或者你的数据库中只有一个结果
6 年多之前 回复
doumian0569
doumian0569 Asped ...我得到一个结果的数组。
6 年多之前 回复
dongxue9997
dongxue9997 你有一个结果的数组吗? 或者只是一个对象?
6 年多之前 回复
dongmei1828
dongmei1828 asped ...在这样做时只撤回了一个结果......查询中有什么不对。
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐