我已经尝试了各种解决方案,但它仍然给出了下面显示的错误 - 我没有想法(我也尝试更换“。$ _ SESSION ['plz']。” - 一个简单的数字部分,但没有改变任何东西:
$profilequery = "SELECT teacher.teacher_id, teacher.plz, teacher.ort,
teacher.teacherprename, teacher.teachername, hours_week.hours_week, teacher.day_times,
teacher.grade, teacher.study, teacher.references, teacher_subjects.qualification,
teacher_subjects.subjectgrade, teacher_subjects.subjectqualification,
ABS (teacher.plz - ".$_SESSION['plz'].") AS distance FROM (
(
SELECT teacher.teacher_id, teacher.plz, teacher.ort,
teacher.teacherprename, teacher.teachername, hours_week.hours_week, teacher.day_times,
teacher.grade, teacher.study, teacher.references, teacher_subjects.qualification,
teacher_subjects.subjectgrade, teacher_subjects.subjectqualification FROM teacher LEFT JOIN
teacher_subjects
ON
teacher.teacher_id = teacher_subjects.teacher_id
LEFT JOIN
hours_week
ON
teacher.hours_id = hours_week.hours_id WHERE teacher.plz >= '".$_SESSION['plz']."'
AND teacher_subjects.status_id = '1' ORDER BY teacher.plz LIMIT 10
) UNION ALL (
SELECT teacher.teacher_id, teacher.plz, teacher.ort,
teacher.teacherprename, teacher.teachername, hours_week.hours_week, teacher.day_times,
teacher.grade, teacher.study, teacher.references, teacher_subjects.qualification,
teacher_subjects.subjectgrade, teacher_subjects.subjectqualification FROM teacher LEFT JOIN
teacher_subjects
ON
teacher.teacher_id = teacher_subjects.teacher_id
LEFT JOIN
hours_week
ON
teacher.hours_id = hours_week.hours_id WHERE teacher.plz >= '".$_SESSION['plz']."'
AND teacher_subjects.status_id = '1' ORDER BY teacher.plz LIMIT 10
)
) AS a
ORDER BY distance
LIMIT 10";
它把它扔向我:
找不到列:1054在“字段列表”中的未知列'plz'在..
中
我真的没有看到任何错误,是否有人知道可能导致问题的原因?我感谢任何帮助!
编辑:删除了“老师”。和“teacher_subjects。”部分在查询中(也尝试仅在主查询或子查询中删除它):$profilequery = "SELECT teacher_id, plz, ort,
teacherprename, teachername, hours_week, day_times,
grade, study, references, qualification,
subjectgrade, .subjectqualification,
ABS (teacher.plz - ".$_SESSION['plz'].") AS distance FROM (
(
SELECT teacher_id, plz, ort,
teacherprename, teachername, hours_week, day_times,
grade, study, references, qualification,
subjectgrade, .subjectqualification FROM teacher LEFT JOIN
teacher_subjects
ON
teacher.teacher_id = teacher_subjects.teacher_id
LEFT JOIN
hours_week
ON
teacher.hours_id = hours_week.hours_id WHERE teacher.plz >= '".$_SESSION['plz']."'
AND teacher_subjects.status_id = '1' ORDER BY teacher.plz LIMIT 10
) UNION ALL (
SELECT teacher_id, plz, ort,
teacherprename, teachername, hours_week, day_times,
grade, study, references, qualification,
subjectgrade, .subjectqualification FROM teacher LEFT JOIN
teacher_subjects
ON
teacher.teacher_id = teacher_subjects.teacher_id
LEFT JOIN
hours_week
ON
teacher.hours_id = hours_week.hours_id WHERE teacher.plz >= '".$_SESSION['plz']."'
AND teacher_subjects.status_id = '1' ORDER BY teacher.plz LIMIT 10
)
) AS a
ORDER BY distance
LIMIT 10";
它抛出了这个:
完整性约束违规:1052字段列表中的'lehrer_id'列不明确'
现在我想这与LEFT JOIN部分有关(在使用此部分之前,只选择了plz
和lehrer_id
。我还尝试了LEFT JOIN
部分在最后(在ALL UNION部分之后)但它仍然会抛出错误。
编辑2:解决了! - >必须提供查询以及LEFT JOIN
的唯一名称(as b
等),然后正确引用它们 - >
$profilequery = "SELECT n.teacher_id, n.plz, n.ort,
n.teacherprename, n.teachername, n.hours_week, n.day_times,
n.grade, n.study, n.qualification,
n.subjectgrade, n.subjectqualification,
ABS (n.plz - ".$_SESSION['plz'].") AS distance FROM (
(
SELECT b.teacher_id, b.plz, b.ort,
b.teacherprename, b.teachername, sw.hours_week, b.day_times,
b.grade, b.study lf.qualification,
lf.subjectgrade, lf.subjectqualification FROM teacher as b LEFT JOIN
teacher_subjects as lf
ON
teacher.teacher_id = teacher_subjects.teacher_id
LEFT JOIN
hours_week as sw
ON
b.hours_id = sw.hours_id WHERE b.plz >= '".$_SESSION['plz']."'
AND lf.status_id = '1' ORDER BY b.plz LIMIT 10
) UNION ALL (
SELECT b.teacher_id, b.plz, b.ort,
b.teacherprename, b.teachername, sw.hours_week, b.day_times,
b.grade, b.study lf.qualification,
lf.subjectgrade, lf.subjectqualification FROM teacher as b LEFT JOIN
teacher_subjects as lf
ON
teacher.teacher_id = teacher_subjects.teacher_id
LEFT JOIN
hours_week as sw
ON
b.hours_id = sw.hours_id WHERE b.plz >= '".$_SESSION['plz']."'
AND lf.status_id = '1' ORDER BY b.plz LIMIT 10
)
) AS n
ORDER BY distance
LIMIT 10";
答案 0 :(得分:1)
您的子查询必须附加plz
列,否则将无法在主查询中看到,因此:
$profilequery = "SELECT teacher_id, ABS (plz - ".$_SESSION['plz'].") AS distance FROM (
(
SELECT teacher_id, plz FROM teacher WHERE plz >= '".$_SESSION['plz']."' ORDER BY teacher.plz LIMIT 10
) UNION ALL (
SELECT teacher_id, plz FROM teacher WHERE plz >= '".$_SESSION['plz']."' ORDER BY plz DESC
LIMIT 10
)
) AS a
ORDER BY distance
LIMIT 10";
答案 1 :(得分:0)
见上面的更新文章(编辑2) - >必须提供查询以及LEFT JOIN的唯一名称(如b等),然后正确引用它们