试图找出这两个查询是否有重叠
下面的 PHP/MYSQL 代码可以工作,但是它很笨重;两次查询,数据转换成数组,数组进行比较!
(注意这是一个函数,因为它返回是否有重叠)
$manager_sql = "SELECT league_id FROM league_members WHERE manager = 1 AND member_active = 1 AND player_id = $mi";
$player_sql = "SELECT league_id FROM league_members WHERE member_active = 1 AND player_id = $pi";
$manager_result = mysql_query($manager_sql) or die(mysql_error());
$manager_leagues = array();
while($sqlrow = mysql_fetch_array($manager_result)) {
extract( $sqlrow );
$manager_leagues[] = $league_id;
}
$player_result = mysql_query($player_sql) or die(mysql_error());
$player_leagues = array();
while($sqlrow = mysql_fetch_array($player_result)) {
extract( $sqlrow );
$player_leagues[] = $league_id;
}
$result = array_intersect($manager_leagues, $player_leagues);
return count($result) > 0;
以下是一次性进行查询的一些尝试:
(SELECT league_id FROM league_members WHERE manager = 1 AND member_active = 2 AND player_id = '$mi')
UNION ALL
(SELECT league_id FROM league_members WHERE member_active = 1 AND player_id = '$pi')
似乎工作,但返回太多行:
SELECT league_id FROM league_members WHERE league_id IN (
SELECT league_id FROM league_members WHERE manager = 1 AND member_active = 1 AND player_id = '$mi'
) AND league_id IN (
SELECT league_id FROM league_members WHERE member_active = 1 AND player_id = '$pi'
)