ai didi

php - MySQL 查询 - 连接两个表产生重复结果

转载 作者:可可西里 更新时间:2023-11-01 07:33:14 24 4
gpt4 key购买 nike

我正在 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";

由于某种原因,此查询返回重复记录。如果有人有任何见解,我将不胜感激。

这是两个表(挑战和验证)的结构:

挑战表:

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;

感谢您的帮助,如果您需要更多信息,请告诉我。

最佳答案

你必须添加条件:

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

关于php - MySQL 查询 - 连接两个表产生重复结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13056983/

24 4 0
文章推荐: php - 使用 PHP 将大型数据库转储为 JSON
文章推荐: mysql - 将一列中的数据分组到单个单元格中输出 mySQL 结果
文章推荐: mysql - 带有多个定界符的 sql-maven-plugin
文章推荐: php - 选择链接表中缺少的行
可可西里
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com