尝试查找这两个查询是否有重叠
以下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'
)
答案 0 :(得分:0)
尝试此查询:
$sql = "SELECT ls1.league_id FROM league_members AS ls1
INNER JOIN league_members AS ls2 ON ls1.league_id = ls2.league_id
WHERE ls1.manager = 1 AND ls1.member_active = 1 AND ls1.player_id = '".$mi."'
AND ls2.member_active = 1 AND ls2.player_id = '".$pi."'"
注意:
答案 1 :(得分:0)
您希望找到任何同时拥有$mi
作为经理且$pi
作为其中一名玩家的联盟?
SELECT DISTINCT l.league_id FROM league_members l JOIN league_members l2 ON l.league_id = l2.league_id AND l.member_active = 1 AND l2.member_active = 1 AND l.manager = 1 AND l2.manager = 0 WHERE l.player_id = $mi AND l2.player_id = $pi;
答案 2 :(得分:0)
试
"SELECT league_id FROM league_members WHERE manager = 1 AND member_active = 1 AND (player_id = '".$mi."' OR player_id = '".$pi."')";
答案 3 :(得分:0)
请尝试以下它会工作。如果没有,那么请让我知道你遇到了什么样的问题。
SELECT league_id FROM league_members WHERE league_id IN( SELECT league_id FROM league_members WHERE(member_active = 1 AND(player_id ='$ mi'或player_id ='$ pi')AND manager = 1)
答案 4 :(得分:0)
我显然没有尝试过这个,但我建议使用distinct
关键字添加最后一个查询:
SELECT distinct 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'
)
答案 5 :(得分:0)
怎么样?
SELECT league_id
FROM league_members
WHERE (manager = 1 AND member_active = 1 AND player_id = $mi) OR
(member_active = 1 AND player_id = $pi)
GROUP BY league_id
HAVING COUNT(*) = 2