2012-10-24 99 views
4

我在PHP中運行以下MySQL查詢。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"; 

此查詢由於某種原因返回重複記錄。如果有人有任何見解,我將不勝感激。

下面是兩個表(挑戰和驗證)的結構:

挑戰表:

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; 

感謝您的幫助,如果你需要更多的信息,請讓我知道。

+0

您應該避免使用'SELECT *'(使用列列表)並使用隱式聯接,因爲它們都是不好的練習。 – Kermit

+0

@njk你爲什麼認爲'選擇*'不好的做法? –

+0

該查詢值得正確格式化。 –

回答

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"; 
+0

如果您要添加條件,爲什麼不將查詢轉換爲使用ANSI JOIN語法而不是逗號? – Taryn

+0

@bluefeet爲什麼不能:)?謝謝我編輯了這篇文章。 – Parado

+0

使用ANSI標準+1。 – Kermit