我必须从两个表中进行查询,例如,这是我的“学生”表的表结构:
-------------------------------------------
id | Name | mom_job_id | dad_job_id
-------------------------------------------
1 | Test1 | 1 | 2
2 | Test2 | 3 | 1
-------------------------------------------
我有这样的“工作”表:
---------------------------
id | job_name
---------------------------
1 | designer
2 | writer
3 | programmer
---------------------------
我想在学生表上选择一条记录,并用“工作表”上的工作名称替换“mom_job_id”和“dad_job_id”
我试过这个查询:
SELECT student.id, job.job_name as mom_job, job.job_name as dad_job
FROM student
INNER JOIN job ON mom_job_id = job.id
WHERE id=1
该查询工作正常,但爸爸的工作设置为与妈妈的工作相同,所以我添加了另一个 INNER JOIN 并使查询如下:
SELECT student.id, job.job_name as mom_job, job.job_name as dad_job
FROM student
INNER JOIN job ON mom_job_id = job.id
INNER JOIN job ON dad_job_id = job.id
WHERE id=1
查询有效,但未返回任何记录。那么我该如何解决呢?我相信问题来自job.id