MySQL-通过匹配引用从多个表中获取数据

时间:2015-08-11 18:41:43

标签: mysql database join phpmyadmin relationship

我尝试使用连接查询从不同的表中获取数据。但即使有数据,它也没有返回结果。

学生主表包含有关学生的详细信息。学生的卷号是此表中的主键。卷号将添加到所有结果表中(即first_term_results,second_term_results等)以供参考。

因此,为了获取学生的详细信息,他的第一和第二学期结果,我使用以下查询,但没有给出预期的结果。我正在尝试获取与roll_no匹配的详细信息。

SELECT a.*, b .*,c.*
FROM student_master AS a
INNER JOIN first_term_results AS b
INNER JOIN second_term_results AS c
ON a.roll_no = b.roll_no = c.roll_no
WHERE a.roll_no = '53'
AND b.roll_no='53'
AND c.roll_no = '53'

有人可以帮我纠正这个问题以获得我期待的结果吗?。如果我不清楚,也请告诉我。我可以解释。

提前致谢。

3 个答案:

答案 0 :(得分:1)

将您的查询修改为

SELECT a.*, b.*,c.*
FROM student_master AS a
INNER JOIN first_term_results AS b ON a.roll_no = b.roll_no
INNER JOIN second_term_results AS c ON a.roll_no = c.roll_no
WHERE a.roll_no = '53'

答案 1 :(得分:1)

这应符合您的意愿:

SELECT a.*, b .*,c.*
FROM student_master AS a
LEFT JOIN first_term_results AS b ON(a.roll_no = b.roll_no)
LEFT JOIN second_term_results AS c ON( a.roll_no = c.roll_no)
WHERE a.roll_no = '53'

您是否检查过驱动程序是否返回错误?

答案 2 :(得分:1)

在将查询格式化为可读的内容之后我得到了这个

SELECT a.*, b .*,c.* 
FROM   student_master AS a 
  INNER JOIN first_term_results AS b 
  INNER JOIN second_term_results AS c ON a.roll_no = b.roll_no = c.roll_no 
WHERE   a.roll_no = '53' 
AND     b.roll_no='53'
AND     c.roll_no = '53'

对我来说有点奇怪, 我会选择这样的东西

SELECT a.*, b .*,c.* 
FROM   student_master AS a 
  INNER JOIN first_term_results AS b on a.roll_no = b.roll_no 
  INNER JOIN second_term_results AS c ON a.roll_no = c.roll_no 
WHERE   a.roll_no = '53'