Mysql SQL查询左联接表
我昨天肯定实现了,如果另一个表没有值,我就使用了Mysql SQL查询左联接表,mysql,sql,Mysql,Sql,我昨天肯定实现了,如果另一个表没有值,我就使用了LEFT JOIN。 我现在的问题是,如果您不确定哪个表有值,哪个表有值,该怎么办 我试过这个问题 $query ="SELECT record.student_name,record.student_id,record.student_finger,record.student_section,record.activity_type,record.activity_title,record.score,record.teacher_id,rec
LEFT JOIN
。我现在的问题是,如果您不确定哪个表有值,哪个表有值,该怎么办 我试过这个问题
$query ="SELECT record.student_name,record.student_id,record.student_finger,record.student_section,record.activity_type,record.activity_title,record.score,record.teacher_id,record.activity_id,record.subject_id,attendance.status,attendance.date
FROM tbl_record record
LEFT JOIN tbl_attendance attendance on record.student_id=attendance.student_number
WHERE record.subject_id='$subject_id' and record.teacher_id='$teacher_id' and record.student_section='$section';";
但不幸的是,如果我的记录表是空的,它将不会显示任何内容
我想要达到的是,如果
table\u record
为空且tbl\u考勤
不为空,则将在tbl\u考勤
中显示记录,如果
table\u考勤
为空,而table\u记录
不为空,它将在table\u记录中显示记录。将左连接更改为完全外部连接,调整WHERE子句并让我们知道它是如何进行的
SELECT
record.student_name,
coalesce(record.student_id, attendance.student_number),
record.student_finger,
record.student_section,
record.activity_type,
record.activity_title,
record.score,
record.teacher_id,
record.activity_id,
record.subject_id,
attendance.status,
attendance.date
FROM tbl_record record
FULL OUTER JOIN tbl_attendance attendance on record.student_id=attendance.student_number
WHERE (record.subject_id='$subject_id' and record.teacher_id='$teacher_id'
and record.student_section='$section')
or record.subject_id is null;
这是一个MySql草案版本,不支持完全外部联接。您可以在右侧联接部分中用空值替换所有record.*字段:
SELECT
record.student_name,
record.student_id,
record.student_finger,
record.student_section,
record.activity_type,
record.activity_title,
record.score,
record.teacher_id,
record.activity_id,
record.subject_id,
attendance.status,
attendance.date
FROM tbl_record record
LEFT JOIN tbl_attendance as attendance on
record.student_id = attendance.student_number
WHERE (record.subject_id='$subject_id' and record.teacher_id='$teacher_id'
and record.student_section='$section')
UNION
SELECT
record.student_name,
attendance.student_number,
record.student_finger,
record.student_section,
record.activity_type,
record.activity_title,
record.score,
record.teacher_id,
record.activity_id,
record.subject_id,
attendance.status,
attendance.date
FROM tbl_record as record
RIGHT JOIN tbl_attendance as attendance on
record.student_id = attendance.student_number
WHERE record.subject_id is null
;
只需将“左连接”更改为“完全外部连接”
为此,您需要
完全外部连接
。但这没有多大意义,因为不会有任何主题、教师或部门信息,所以当你尝试对其进行过滤时(在你的where
中),你不会得到任何结果。如果您想要X老师的报告,但tbl\u记录中没有任何内容,您希望返回什么?似乎你有一个数据完整性问题,也许我应该把出席人数记录在tbl_记录上。表格模型“事物”或“活动”。这里每个表代表什么?一个名为tbl_record
的表没有告诉我该表要表示什么<代码>tbl_出勤
确实给了我一个线索。如果您在tbl_出勤记录中有一个给定的学生号,那么该学生号怎么可能不在tbl_记录中
?你一定是凭空发明了学生id
价值观。您需要做的是用非技术性语言理解需求,即提出问题。在allOkay,你可能会发现这个问题毫无意义。正如Nick.McDermaid指出的那样,尽管你还需要考虑你的模型,我还是会尝试一下,然后再回来看看结果。出勤率是否不是学生的财产?如果没有学生
参考,是否可以存在出勤
记录?我想不太可能。它还是空的,ATM机我没有tbl_的记录,但我有tbl_的出勤记录。是 啊也许我应该考虑用“<代码> CueSeCE”(Reord.SurvivID,Curn.SurvivsNo.No./Cord>)替换Reord.Cube ID。更新了我的答案(但不是演示)。
SELECT *
FROM tbl_Students S
FULL OUTER JOIN tbl_Attendance A
ON S.StudentID = A.AttendanceStudentID