PHP / MySQL的。在2个表中选择2个常用字段

时间:2017-10-20 14:50:02

标签: php mysql

我正在制作一个关于使用PHP和MySQL的考勤系统的项目。我的SQL语法有问题。我想在“学生”表中显示学生的所有姓名和“考勤”表中的记录,但我继续在屏幕上循环显示5条额外的记录。这就是我到目前为止所做的。

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

1 个答案:

答案 0 :(得分: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;