0

试图找出这两个查询是否有重叠

下面的 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'
)
4

6 回答 6

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. mysql_* 函数已弃用,请使用 mysqli_* 函数或 PDO
  2. 您的查询容易受到 sql 注入的影响,请确保其安全。
于 2013-02-27T06:07:36.693 回答
0

您正在寻找任何既$mi作为经理又$pi作为球员之一的 League_id?

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;
于 2013-02-27T06:08:16.973 回答
0

尝试

"SELECT league_id FROM league_members WHERE manager = 1 AND member_active = 1 AND (player_id = '".$mi."' OR player_id = '".$pi."')";
于 2013-02-27T06:08:47.957 回答
0

请在下面尝试它会起作用。如果没有,请让我知道您遇到了哪种问题。

SELECT League_id FROM League_members WHERE League_id IN(从 League_members 中选择 League_id WHERE (member_active = 1 AND (player_id = '$mi' or player_id = '$pi') AND manager = 1)

于 2013-02-27T06:09:20.250 回答
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'
)
于 2013-02-27T06:11:23.803 回答
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
于 2013-02-27T06:18:36.440 回答