2017-10-20 119 views
0

我正在制作一个关于使用PHP和MySQL的考勤系统的项目。我在我的SQL语法中遇到了麻烦。我想在我的“学生”表中显示所有学生的姓名和“考勤”表中的记录,但是我仍然在屏幕上循环显示5条额外的记录。这是我迄今为止所做的。php/mysql。在2个表中选择2个共同字段

SELECT students.name as StudentName, attendance.timelogin as TimeLogin, attendance.datelogin as DateLogin FROM students, attendance WHERE students.strand LIKE '%TVL%' AND students.gender ='Male' AND attendance.datelogin LIKE '%{$month}%' AND attendance.timelogin >= '12' ORDER BY students.name ASC; 

screen-output

+0

从我的“学生”表中的学生* –

+0

我没有看到你加入了2张表。 – isaace

+0

我想显示P为目前,L为晚和A为缺席在日期编号列从HTML表格。 –

回答

0

你需要添加一个连接条件正常,所以如果你有在考勤表中的学生表的主键Id和另一外国键studentid使用它。并使用ANSI连接语法如下:

SELECT 
    s.name as StudentName, 
    a.timelogin as TimeLogin, 
    a.datelogin as DateLogin 
    UPPER(LEFT(status, 1)) AS Status 
FROM students AS s 
INNER JOIN attendance AS a ON s.Id = a.StudentId 
WHERE s.strand LIKE '%TVL%' 
    AND s.gender ='Male' 
    AND a.datelogin LIKE '%{$month}%' 
    AND a.timelogin >= '12' 
ORDER BY s.name ASC; 
+0

如何使用此代码中的变量a和s“FROM students AS s INNER JOIN attendance AS a ??”? –

+1

a和s只是表格名称的别名 –

+0

@JanMichaelBesinga就像Lio说的那样,它只是用别名来清除它们并使用表格的全名。 – 2017-10-20 15:06:07