我的Sql技能缺乏,非常感谢帮助。在字段相同的情况下拉出所有记录的最有效方法是什么?
例如,我的表格名称是' games'并与表格'控制台'
相关联如何使用相同(重复)'名称'提取所有游戏记录,保留关联并能够访问所有字段。
基本上修改它只显示重复:
$this->set('games', $this->Game->find('all', array('order' => array('Game.name' => 'asc'))));
有没有一种简单的方法可以使用cake执行此操作,或者如何使用query()通过sql执行此操作?
提前多多赞赏!
此致 灰
答案 0 :(得分:0)
我不确定蛋糕是否有内置的方式来代表这个,但你可以用这个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')));
答案 1 :(得分:0)
尝试使用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
行进行了评论。看起来你想要所有的结构,所以你无论如何也不需要它们。
EDIT2。消除字段名称可能存在的歧义。
答案 2 :(得分: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';
}