如何在mysql的字段中从包含字段id(1,2,3)的字段中获取记录?

时间:2015-06-23 09:56:55

标签: php mysql

我有2个学生表和ex_students

(1) Table name: students
+----+--------+
| id | name   |
+----+--------+
|  1 | Alex   |
|  2 | Bill   |
|  3 | Cath   |
|  4 | Dale   |
|  5 | Evan   |
+----+--------+

ex_students的其他表

(2) Table name: ex_students
+----+--------+-------+
| id | parent | s_ids |
+----+--------+-------+
|  1 | Abcs   | 1,2,3 |
|  2 | Bcde   | NULL  |
|  3 | Cdef   | NULL  |
|  4 | Defg   | NULL  |
|  5 | Efgh   | NULL  |
+----+--------+-------+

最后我想要像

这样的结果
+----+--------+-------+
| id | name   | status|
+----+--------+-------+
|  1 | Alex   |   1   |
|  2 | Bill   |   1   |
|  3 | Cath   |   1   |
|  4 | Dale   |  NULL |
|  5 | Evan   |  NULL |
+----+--------+-------+

如何使用mysql查询获取记录? 我尝试下面的查询,但不工作..

  

SELECT students.id,students.name,IF(COALESCE(ex_students.id,1)= 1,'1','2')作为状态来自学生LEFT JOIN ex_students在FIND_IN_SET上(ex_students.s_ids,students.id )> 0分组由students.id

1 个答案:

答案 0 :(得分:1)

更改

FIND_IN_SET(ex_students.s_ids, students.id)

FIND_IN_SET(students.id, ex_students.s_ids)