4

PHPで次のMySQLクエリを実行しています。

"SELECT * 
FROM `challenges`,`verifications` 
WHERE (`challenges`.`user_id`='".$this->record['id']."' OR `challenges`.`opponent_id`='".$this->record['id']."') 
    AND `challenges`.`is_verified`='0' 
    AND (`challenges`.`status`='in-progress' OR `challenges`.`status`='pending') 
    AND 
    (
        (`verifications`.`user_id`='".$this->record['id']."' OR `verifications`.`opponent_id`='".$this->record['id']."') 
        AND (`verifications`.`user_verified`!=NULL AND `verifications`.`opponent_verified`=NULL) 
    ) 
LIMIT 100";

このクエリは、何らかの理由で重複レコードを返しています。誰かが何か洞察を持っているなら、私はそれを大いに感謝します。

2つのテーブル(チャレンジと検証)の構造は次のとおりです。

課題表:

CREATE TABLE `challenges` (
  `id` int(11) NOT NULL auto_increment,
  `wager` int(11) NOT NULL,
  `type` varchar(255) NOT NULL,
  `user_id` int(11) NOT NULL,
  `opponent_id` int(11) NOT NULL,
  `start_date` date NOT NULL,
  `date_created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `start_time` time NOT NULL,
  `is_verified` tinyint(1) NOT NULL default '0',
  `status` varchar(255) NOT NULL default 'pending',
  `winner_id` int(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

検証表:

CREATE TABLE `verify` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL,
  `opponent_id` int(11) NOT NULL,
  `challenge_id` int(11) NOT NULL,
  `user_verified` int(11) default NULL,
  `opponent_verified` int(11) default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `challenge_id` (`challenge_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;

ご協力いただきありがとうございます。さらに情報が必要な場合はお知らせください。

4

1 に答える 1

4

条件を追加する必要があります:

challenges.id = verify.challenge_id

以下のwhere句へ

"SELECT * 
 FROM `challenges`,`verifications` 
 WHERE `challenges`.`id` = `verify`.`challenge_id`
 AND (`challenges`.`user_id`='".$this->record['id']."' 
      OR `challenges`.`opponent_id`='".$this->record['id']."') 
 AND `challenges`.`is_verified`='0' 
 AND (`challenges`.`status`='in-progress' OR `challenges`.`status`='pending') 
 AND ( (`verifications`.`user_id`='".$this->record['id']."' 
         OR `verifications`.`opponent_id`='".$this->record['id']."') 
        AND (`verifications`.`user_verified`!=NULL 
              AND `verifications`.`opponent_verified`=NULL) 
     ) 
 LIMIT 100";

または使用ANSI-92

"SELECT * 
 FROM `challenges` as `challenges`
 JOIN `verifications` as `verifications` on `challenges`.`id` = `verify`.`challenge_id`    
 WHERE (`challenges`.`user_id`='".$this->record['id']."' OR `challenges`.`opponent_id`='".$this->record['id']."') 
        AND `challenges`.`is_verified`='0' 
        AND (`challenges`.`status`='in-progress' OR `challenges`.`status`='pending') 
        AND 
        (
            (`verifications`.`user_id`='".$this->record['id']."' OR `verifications`.`opponent_id`='".$this->record['id']."') 
            AND (`verifications`.`user_verified`!=NULL AND `verifications`.`opponent_verified`=NULL) 
        ) 
        LIMIT 100";
于 2012-10-24T20:21:53.087 に答える