0

我缺乏 Sql 技能,非常感谢您的帮助。提取字段相同的所有记录的最有效方法是什么?

例如,我的表名称是“游戏”,并且与表“控制台”有关联

如何提取具有相同(重复)“名称”的所有游戏记录、保留关联并能够访问所有字段。

基本上将其修改为仅显示重复项:

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

有没有一种简单的方法可以使用 cake 来做到这一点,或者我将如何使用 query() 通过 sql 来做到这一点?

非常感谢提前!

问候,阿什

4

3 回答 3

1

尝试使用 Konerak 查询,但它只在小表上有效,在我的测试库上,它有 2k 条记录,它在 sql 客户端上崩溃并在 php 上出现内存错误(我使用非索引字段进行测试)。如果它适合你:

$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));

/*

该解决方案使用 2 个查询,但我设法对其进行了很好的测试:

$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')));

让我知道这是否对您不起作用以及您遇到了什么问题。

编辑。我评论了这些contain台词。似乎你想要所有的结构,所以无论如何你都不需要它们。

编辑2。消除字段名称可能存在的歧义。

于 2012-08-30T14:26:16.693 回答
0

我不确定 cake 是否有内置的方式来表示这一点,但你可以用这个 MySQL 方法来做到这一点。

$this->set('games', $this->Game->query('SELECT name, COUNT(*) as count FROM games GROUP BY name HAVING count > 1');

您可以在选择中包含您想要的任何字段。


更新

这将是 CakePHP 的做法,结果的格式更好一些:

$this->set('games', $this->Game->find('all',array('fields'=>array('name','COUNT(*) as count'), 'group'=>'name', 'having'=>'count > 1')));
于 2012-08-30T14:03:05.147 回答
0
public function admin_duplicate_account() {
    $this->User->unBindModel(array('hasOne' => array('Admin')));
    if ($this->request->is('post') || $this->request->is('put')) {
        // pr($this->request->data); exit;
        if ($this->request->data['User']['email'] == 1) {
            $arr_having = array();
            $arr_having['fields'] = 'User.email';
            $arr_having['group'] = 'User.email HAVING COUNT(*) > 1';

            $regs = $this->User->find('all', $arr_having, array('conditions' => array('User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive'))));
            $names = array();
            foreach ($regs as $reg) {

                if ($reg['User']['email']) {
                    $names[] = $reg['User']['email'];
                }
            }

            $this->set('allusers', $this->User->find('all', array(
                        'conditions' => array('User.email' => $names, 'User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive')))));
        }


        if ($this->request->data['User']['first_name'] == 1) {
            $arr_having = array();
            $arr_having['fields'] = 'PersonalInformation.first_name';
            $arr_having['group'] = 'PersonalInformation.first_name HAVING COUNT(*) > 1';

            $regs = $this->User->find('all', $arr_having, array('conditions' => array('User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive'))));

            //pr($regs); exit;
            $names = array();
            foreach ($regs as $reg) {

                if ($reg['PersonalInformation']['first_name']) {
                    $names[] = $reg['PersonalInformation']['first_name'];
                }
                //$names[] = $reg['PersonalInformation']['first_name'];
            }
            //pr($names); 
            $data = $this->User->find('all', array(
                'conditions' => array('PersonalInformation.first_name' => $names, 'User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive'))));
            //pr($data); exit;

            $this->set('allusers', $data);
        }



        if ($this->request->data['User']['last_name'] == 1) {

            $arr_having = array();
            $arr_having['fields'] = 'PersonalInformation.last_name';
            $arr_having['group'] = 'PersonalInformation.last_name HAVING COUNT(*) > 1';

            $regs = $this->User->find('all', $arr_having, array('conditions' => array('User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive'))));

            //pr($regs); exit;
            $names = array();
            foreach ($regs as $reg) {

                if ($reg['PersonalInformation']['last_name']) {
                    $names[] = $reg['PersonalInformation']['last_name'];
                }
                //$names[] = $reg['PersonalInformation']['first_name'];
            }
            //pr($names); 
            $data = $this->User->find('all', array(
                'conditions' => array('PersonalInformation.last_name' => $names, 'User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive'))));
            //pr($data); exit;

            $this->set('allusers', $data);
        }


        if ($this->request->data['User']['primary_phone'] == 1) {

            $arr_having = array();
            $arr_having['fields'] = 'PersonalInformation.primary_phone';
            $arr_having['group'] = 'PersonalInformation.primary_phone HAVING COUNT(*) > 1';

            $regs = $this->User->find('all', $arr_having, array('conditions' => array('User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive'))));

            //pr($regs); exit;
            $names = array();
            foreach ($regs as $reg) {

                if ($reg['PersonalInformation']['primary_phone']) {
                    $names[] = $reg['PersonalInformation']['primary_phone'];
                }

            }
            //pr($names); 
            $data = $this->User->find('all', array(
                'conditions' => array('PersonalInformation.primary_phone' => $names, 'User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive'))));
            //pr($data); exit;

            $this->set('allusers', $data);
        }
        if ($this->request->data['User']['dob'] == 1) {


            $arr_having = array();
            $arr_having['fields'] = 'PersonalInformation.dob';
            $arr_having['group'] = 'PersonalInformation.dob HAVING COUNT(*) > 1';

            $regs = $this->User->find('all', $arr_having, array('conditions' => array('User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive'))));

            //pr($regs); exit;
            $names = array();
            foreach ($regs as $reg) {

                if ($reg['PersonalInformation']['dob']) {
                    $names[] = $reg['PersonalInformation']['dob'];
                }
            }
            //pr($names); 
            $data = $this->User->find('all', array(
                'conditions' => array('PersonalInformation.dob' => $names, 'User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive'))));
            //pr($data); exit;

            $this->set('allusers', $data);
        }
    } else {

        $this->set('allusers', $this->User->find('all', array(
                    'conditions' => array('User.role' => array('U', 'P'), 'User.user_status' => array('active', 'lead', 'inactive')))));
    }

    $this->layout = 'admin';
}
于 2015-03-03T07:06:30.100 回答