仅针对特定行的mysql连接

时间:2016-08-24 18:31:59

标签: php mysql codeigniter activerecord

我有三张桌子

评论表

---------------------------------------------------------------------------
id    |    commented_by       | comment          | commented_by
---------------------------------------------------------------------------
1     |    asdf156df5s4fd     | student comment  |  student
2     |    klsdfjklsdfno      | student comment  |  student
3     |    jafkadfjlkdnvwa    | teacher comment  |  teacher
4     |    adsjflandsfinndfs  | student comment  |  student
5     |    dsf5614dsf8wef4    | teacher comment  |  teacher
---------------------------------------------------------------------------

教师表

id    |    teacher_id       | teacher_name | teaches
---------------------------------------------------------------------------
1     |    jafkadfjlkdnvwa  | abc cde      |  maths
2     |    dsf5614dsf8wef4  | egc kjl      |  science
.
.
.
---------------------------------------------------------------------------

学生表

id    |    student_id        | student_name |
---------------------------------------------------------------------------
1     |    asdf156df5s4fd    | student_1    |
2     |    klsdfjklsdfno     | student_2    |
3     |    adsjflandsfinndfs | student_3    |
.
.
---------------------------------------------------------------------------

现在,我们希望mysql join可以返回类似这样的内容

id    |    comment_id         | comment_by |  name | teaches
---------------------------------------------------------------------------
1     |    asdf156df5s4fd     | student    |  student_1   | null
2     |    klsdfjklsdfno      | student    |  student_2   | null
3     |    jafkadfjlkdnvwa    | teacher    |  abc cde     | maths
4     |    adsjflandsfinndfs  | student    |  student_3   | null
5     |    dsf5614dsf8wef4    | teacher    |  egc kjl     | science
---------------------------------------------------------------------------

无法使用codeigniter活动记录找到我们可以实现此目的的确切查询

2 个答案:

答案 0 :(得分:0)

这是真正的脏表方案,但这是您想要的查询:

SELECT 
  c.id, 
  commented_by as comment_id, 
  commented_by, 
  IF(commented_by = 'student', student_name, teacher_name) as name, 
  teaches
FROM comments c
LEFT JOIN teacher t
  ON t.teacher_id = c.commented_by
 AND c.commented_by = 'teacher'
LEFT JOIN student s
  ON s.student_id = c.commented_by
 AND c.commented_by = 'student'

更好的表格方案:

CREATE TABLE comments (
  comment_id INT NOT NULL AUTO_INCREMENT,
  student_id INT,
  teacher_id INT,
  PRIMARY KEY (comment_id)
);

CREATE TABLE teachers (
  teacher_id INT NOT NULL AUTO_INCREMENT,
  teacher_name VARCHAR(200) NOT NULL,
  teacher_subject VARCHAR(50),
  PRIMARY KEY (teacher_id)
);

CREATE TABLE students (
  student_id INT NOT NULL AUTO_INCREMENT,
  student_name VARCHAR(200) NOT NULL,
  PRIMARY KEY (student_id)
);

SELECT 
  comments.*, 
  IF(student_id IS NOT NULL, student_name, teacher_name) as name, 
  teacher_subject
FROM comments
LEFT JOIN teachers USING(teacher_id)
LEFT JOIN students USING(student_id)

答案 1 :(得分:0)

您需要加入学生和教师表:

select id, comment_id as comment_by , commented_by, 
( case when commented_by = 'teacher' THEN teacher_name else student_name ) AS name, teaches
from comments c left join teacher t ON t.teacher_id = c.commented_by left 
join student s ON s.student_id = c.commented_by