2017-06-10 47 views
0

我想从给定学年和班级的不同表中获得学生的详细信息。我用 查询:为什么我在'on clause'错误中得到未知列'list_class.pk_class_id'?

SELECT 
    list_acad_years.acad_year, 
    a.fk_stu_id, 
    tbl_stu_details.stu_fname, 
    tbl_stu_details.stu_sname, 
    a.fk_section_id, 
    b.fk_class_id, 
    list_class.class_name, 
    list_sections.section_code 
FROM 
    tbl_stu_details, 
    list_class, 
    list_sections, 
    list_acad_years 
INNER JOIN 
    tbl_stu_class AS a 
ON 
    (
    list_acad_years.pk_acad_year_id = a.fk_year_id 
) 
INNER JOIN 
    tbl_stu_class AS b 
ON 
    (list_class.pk_class_id = b.fk_class_id) 
WHERE 
    (
    list_acad_years.acad_year = '2019' 
) AND(list_class.class_name = '10') 

它显示了以下错误:

#1054 - Unknown column 'list_class.pk_class_id' in 'on clause' 

我的表中的列是:

tbl_stu_class:

pk_stu_cls_id`, `fk_stu_id`, `fk_year_id`, `fk_class_id`, `fk_section_id`, `current_yr` 

list_class :

`pk_class_id`, `class_name`, `class_code`, `fk_user_id` 

list_sections:

pk_section_id`, `section_code`, `section_description`, `fk_user_id` 

list_acad_years:

`pk_acad_year_id`, `acad_year`, `acad_year_code`, `fk_user_id` 

tbl_stu_details:

`pk_stu_id`, `stu_id`, `username`, `stu_fname`, `stu_mname`, `stu_sname` 

list_sections:

`pk_section_id`, `section_code`, `section_description`, `fk_user_id` 

为什么它说未知列时列存在? 这将是很大的帮助,如果你能帮助我使这个查询更好... 在此先感谢。

+0

您确定这是生成错误的确切查询吗? –

+0

*从不*在'FROM'子句中使用逗号。 *总是*使用正确的,明确的'JOIN'语法。 –

+0

是的,我刚刚从我的编辑器中复制了查询,这个查询生成了提到的错误 –

回答

0

如果您使用多个FROM表,您的内连接将始终连接到from语句中的最后一个表。
我刚刚改变了INNER JOIN的位置

试试这个让我知道它是否有效。

SELECT 
    list_acad_years.acad_year, 
    a.fk_stu_id, 
    tbl_stu_details.stu_fname, 
    tbl_stu_details.stu_sname, 
    a.fk_section_id, 
    b.fk_class_id, 
    list_class.class_name, 
    list_sections.section_code 
FROM 
    tbl_stu_details, 
    list_class INNER JOIN 
    tbl_stu_class AS b 
ON 
    (list_class.pk_class_id = b.fk_class_id), 
    list_sections, 
    list_acad_years 
INNER JOIN 
    tbl_stu_class AS a 
ON 
    (
    list_acad_years.pk_acad_year_id = a.fk_year_id 
) 
WHERE 
    (
    list_acad_years.acad_year = '2019' 
) AND(list_class.class_name = '10') 
+0

是这工作(y)谢谢 –

+0

如果这个答案是userfull你可以标记为接受这个答案 – Santosh

+0

你可以接受任何你喜欢的答案。任何在FROM子句中都有逗号的代码只是一个糟糕的SQL例子。 –

1

你的问题是你正在使用旧式的连接。期。更糟糕的是,你将它们与新式连接相结合。

通过逗号不能理解表的名称。这限制了定义的范围。

您似乎知道如何正确书写JOIN。所以,只需修复FROM条款,你的代码应该可以工作。

+0

感谢信息..... –

相关问题