0

这可能很容易解决,但我在这段代码上遇到了麻烦——我是一个自学 PHP 的新手,我想出的代码似乎不想工作。

这些页面用于体育比赛的在线输入系统。裁判的详细信息存储在“club_judges”表中,用户可以通过将它们复制到“competition_judges”中来参加比赛。这是通过表格中的复选框形式完成的。

我想添加功能,使已经加入比赛的评委不会出现在导入表单中,但是我的代码似乎不起作用。我正在使用“bg_number”(运动的标识号)的唯一字段来搜索现有条目。

当前代码:

$existing_judges = mysql_query("SELECT bg_number FROM competition_judges WHERE competition='Test Competition'");
$existing_judges_fetch = mysql_fetch_array($existing_judges);
$existing_judges_array = "('" . implode( "', '", $existing_judges_fetch ) . "');" ; 

$query = "SELECT * FROM club_judges WHERE (`club`='Test Club') AND (`bg_number` NOT IN '$existing_judges_array') ORDER BY name ";
$result = mysql_query($query) or die(mysql_error());

显示错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''('1234567', '1234567');') ORDER BY name' at line 1

供参考 1234567 是我的测试法官的 bg_number。

任何帮助将不胜感激!

4

3 回答 3

0

你为什么不只使用一个查询

SELECT * 
FROM club_judges 
WHERE `club`='Test Club' 
AND `bg_number`
NOT IN (SELECT bg_number FROM competition_judges WHERE competition='Test Competition')
ORDER BY name 
于 2013-02-03T16:28:05.667 回答
0

使用可以像这样使用单个查询:

$query = "SELECT * FROM `club_judges` WHERE `club`='Test Club' 
          AND 
          `bg_number` NOT IN 
              (SELECT `bg_number` FROM `competition_judges` WHERE `competition`='Test Competition')
           ORDER BY `name` ";
$existing_judges_fetch = mysql_fetch_array($existing_judges);
$result = mysql_query($query) or die(mysql_error());
于 2013-02-03T16:28:17.117 回答
0

这是一个JOIN版本

SELECT  a.*
FROM    club_judges a
        LEFT JOIN competition_judges b
            ON  a.bg_number = b.bg_number AND
                b.competition='Test Competition'
WHERE   b.bg_number IS NULL AND
        a.club = 'Test Club'
ORDER   BY a.name

要全面了解联接,请访问以下链接:

于 2013-02-03T16:29:46.100 回答