php - 如果记录不存在,如何比较表并显示?

标签 php mysql

我有一个问题,我想比较 studentvotes 和 student 这两个表。 Image of my data structures

我想显示表“studentvotes”中不存在的“student”的“idno”,但我将基于“syearid”。例如,'idno' like 'c23' 然后在 2015 年投票。但在 2016 年没有投票 'idno' 将显示 'c23' 没有为 2016 年投票。我该怎么做?

这里有一些细节:我可以按年份显示谁已经投票,但我不知道如何按年份显示没有投票的学生,这只是我现在的问题。

这是我所做的:

 $stud = mysql_query("select * from student,studentvotes where student.idno = studenvotes.idno AND student.syearid = studentvotes.syearid AND studentvotes.syearid = '$no'") or die(mysql_error());

最佳答案

尝试这样的事情。我认为这应该有效:

SELECT st.*
FROM student st
LEFT JOIN studentvotes sv 
    ON st.idno = sv.idno AND st.syearid = sv.syearid
WHERE sv.idno IS NULL

关于php - 如果记录不存在,如何比较表并显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34782142/

相关文章:

function - 向两个用户发送确认电子邮件

php - Switch 和 IF 有什么区别?

php - 在 Cakephp 中验证 Bootstrap 表单

python - 为什么 MySQL 连接查询的输出作为嵌套列表项返回?

php - 如何使用 VS Code 启动和调试 php?

php - 应用程序想要从本地主机发送电子邮件

php - 分解/分解文本字符串的最佳分隔符 - @、!、<>、# 或 |

mysql - 使用所有 id 的 IN 语句获取一定数量的行

php - 带有内部选择查询的 MySQL 搜索

MySQL,什么是SQL接口(interface)?