我有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
答案 0 :(得分:1)
更改
FIND_IN_SET(ex_students.s_ids, students.id)
到
FIND_IN_SET(students.id, ex_students.s_ids)