Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:48:57+00:00 2026-06-10T13:48:57+00:00

Is it possible to make sub-queries in ActiveRecord in Yii? i have a query

  • 0

Is it possible to make sub-queries in ActiveRecord in Yii?

i have a query like this:

select * from table1
where table1.field1 in (select table2.field2 from table2)

i’m currently using the fallowing code:

object1::model()->findAll(array('condition'=>'t.field1 in (select table2.field2 from table2)'))

[Edit]
i would like to know if there is a manner to construct the sub-query without using SQL, and without using joins.

Is there any solution ?

and thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:48:59+00:00Added an answer on June 10, 2026 at 1:48 pm

    First find doublets by db fields:

    $model=new MyModel('search');
    $model->unsetAttributes();
    
    $criteria=new CDbCriteria();
    $criteria->select='col1,col2,col3';
    $criteria->group = 'col1,col2,col3';
    $criteria->having = 'COUNT(col1) > 1 AND COUNT(col2) > 1 AND COUNT(col3) > 1';
    

    Get the subquery:

    $subQuery=$model->getCommandBuilder()->createFindCommand($model->getTableSchema(),$criteria)->getText();
    

    Add the subquery condition:

    $mainCriteria=new CDbCriteria();
    $mainCriteria->condition=' (col1,col2,col3) in ('.$subQuery.') ';
    $mainCriteria->order = 'col1,col2,col3';
    

    How to use:

    $result = MyModel::model()->findAll($mainCriteria);
    

    Or:

    $dataProvider = new CActiveDataProvider('MyModel', array(
            'criteria'=>$mainCriteria,
    ));
    

    Source: http://www.yiiframework.com/wiki/364/using-sub-query-for-doubletts/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I am trying to make: sub.example.com/page rewrite to www.example.com/sub/page I have this code
Possible Duplicate: Make error: missing separator I am so stressed out by this silly
Possible Duplicate: Make iPhone app paid version replace free version on install from app
I'd like to know if it is possible to store resources data into sub-bundles
Is it possible to replace a url name like http://mysite.com/sub/ with http://sub.mysite.com using htaccess?
I have a format which is defined like below: format STDOUT = ------------------------------------ |Field1
Possible Duplicate: C# arrays , Getting a sub-array from an existing array Basically I
I have to make an iframe element from java script so it isn't scrollable
Is it possible to make the following in CSS: #subMenue { //this rule apply
Is it possible to make a procedure have a single parameter but the values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.