查询SQL条件显示表1和表2中的所有数据

时间:2016-02-23 17:57:05

标签: mysql

我创建了2个表:

表1: 学生

  • id_student varchar5

  • student_name varchar [30]

表2: 缺席

  • id_absent int [11](pk)

  • id_student varchar [5](fk)

  • date_absent [date]

  • info_absent varchar [15]

我今天要显示缺席,但我想要显示所有数据学生。

实施例

**name | info_absent**

ridwan | present

adesta | present

eko  | null 

desti | null

今天没有这个数据样本可能是eko或desti没有出现或生病。

当我这样查询时:

SELECT sw.student_name,ab.info_absent
    FROM student AS sw 
    left outer join absent AS ab  on
    sw.id_student=ab.id_student
     where ab.date_absent='now()'

这只显示数据

**name | info_absent**

ridwan | present

adesta | present

我希望eko和desti也会显示在这些数据中。

2 个答案:

答案 0 :(得分:1)

这是否正常工作?

SELECT sw.student_name, ab.info_absent FROM student sw 
LEFT JOIN absent ab ON ab.id_student = sw.id_student && ab.date_absent = NOW();

您也可以将info_absent中的NULL替换为您想要的任何内容:

SELECT sw.student_name, IFNULL(ab.info_absent, 'Seems present') FROM student sw 
LEFT JOIN absent ab ON ab.id_student = sw.id_student && ab.date_absent = NOW();

答案 1 :(得分:-1)

为什么不

SELECT sw.student_name,ab.info_absent
FROM student AS sw 
left outer join absent AS ab  on
sw.id_student=ab.id_student