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 8458773
In Process

The Archive Base Latest Questions

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

My Sql skills are lacking and would really appreciate the help. What is the

  • 0

My Sql skills are lacking and would really appreciate the help. What is the most efficient way to pull all records where a field is the same?

For example my tables name is ‘games’ and has an association with the table ‘consoles’

How do I pull all record of games with the same (duplicate) ‘name’, retain associations and be able to access all fields.

Basically modify this to just show duplicates:

$this->set('games', $this->Game->find('all', array('order' => array('Game.name' => 'asc'))));

Is there an easy way to do this using cake or how would I do it through sql using query()?

Much appreciated in advance!

Regards,
Ash

  • 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:10:14+00:00Added an answer on June 10, 2026 at 1:10 pm

    Tried to use Konerak query, it worked but only on small tables, on my test base with 2k records it crashed on sql client and gives a memory error on php (I used a non indexed field for testing). If it works for you:

    $options = array();
    $options['joins'][0]['table'] = 'games';
    $options['joins'][0]['alias'] = 'Game2';
    $options['joins'][0]['conditions'] = array(1 => 1);
    $options['conditions'] = 'Game.name = Game2.name';
    $options['conditions']['NOT'] = 'Game.id = Game2.id';
    $options['order'] = 'Game.name ASC';
    
    //$this->Game->contain();
    $this->set('games', $this->Game->find('all', $options));
    

    /*

    This solutions use 2 queries but I managed to test it just fine:

    $options = array();
    $options['fields'] = 'Game.name';
    $options['group'] = 'Game.name HAVING COUNT(*) > 1';
    
    // get all the names > 1
    //$this->Game->contain();
    $regs = $this->Game->find('all', $options);
    
    // make a nice array with them
    $names = array();
    foreach ($regs as $reg) {
    $names[] = $reg['Game']['name'];
    }
    
    // and search for only those games
    //$this->Game->contain();
    $this->set('games', $this->Game->find('all', array(
                                         'conditions' => array('Game.name' => $names), 
                                         'order' => 'name ASC')));
    

    Let me know if that won’t work for you and what problems you are having.

    edit. I commented the contain lines. Seems you want all the structure so you wouldn’t need them anyway.

    edit2. remove possible ambiguity on field names.

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

Sidebar

Related Questions

Thanks for any help in advance, I can't wrap my SQL skills around this
my SQL skills aren't strong enough to figure this out, hope someone can help.
I'll be the first one to admit my SQL skills are lacking. Now... Let's
I'm trying to knock the rust off my SQL skills and need some help
Hey guys I've written this MS SQL script with my awesome MS SQL skills,
SQL Server 2008 Profiler always profiles all databases. I want to restrict profiling or
I'm trying to do what seems simple, but my SQL skills are limited :)
Please forgive my ignorance and poor SQL programming skills but I am normally a
How would I do this in a stored procedure (SQL 2005): count = select
Ok.. So I'm trying to improve my SQL skills and have a question. Here

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.