这可能很容易解决,但是我遇到了这段代码的问题 - 我是一个自学新手用PHP,而我提出的代码似乎并不想工作。< / p>
这些页面适用于体育比赛的在线录入系统。评委的详细信息存储在表“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。
非常感谢任何帮助!
答案 0 :(得分: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
答案 1 :(得分: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());
答案 2 :(得分: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
要充分了解联接知识,请访问以下链接: