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

  • SEARCH
  • Home
  • 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 6548337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:56:20+00:00 2026-05-25T11:56:20+00:00

This question is similar to Find conditions like ‘NOT EXISTS’ except that it’s working

  • 0

This question is similar to Find conditions like ‘NOT EXISTS’ except that it’s working with a hasMany relationship.

The tables are:

questions
id bigint
…

answers
id bigint
question_id bigint
…

The relationship is questions hasMany answers.

The query is to look for question ids that have no answers.

SQL might look like

select id from questions where not exists 
    (select * from answers where answers.question_id = questions.id)

The quick way is to just run a query() with the statement, but I’d like to know if there is a CakePHP way.

I’d like to avoid a NOT IN scenario, since that may result in two hits to the database; One to get all question ids for questions that do have answers, and the second to get all question ids for questions that don’t have answers.

The other way might be to put the whole where clause in the conditions array as a single entry. I’m just not sure if that is best practice.

  • 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-05-25T11:56:21+00:00Added an answer on May 25, 2026 at 11:56 am

    Without having to alter the database, I ended up using the following query:

    $questions = $this->Questions->find('all', array(
        'fields' => array('id'),
        'conditions' => array('not exists '.
            '(select id from answers '.
            'where answers.question_id = '.
            'Question.id)'
         )
    ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question is similar to several previous ones, but I can't find
This question is similar to this question , bit the big difference is that
This question is similar to Getting Emacs fill-paragraph to play nice with javadoc-like comments
This question is similar to this other one , with the difference that the
So in another controller, I use a find like this: @entries = Entry.find(:all, :conditions
This question is similar to this one , but not a duplicate because I'm
My question is similar to this question and since I was unable to find
This question is very similar to How to find a duplicate element in an
This question is similar to my last question . Except this time I'm using
My question is similar to this question . Though I have not been able

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.