简体   繁体   中英

how to get records from field having contain field ids like (1,2,3) in a field in mysql?

I have 2 tables students and ex_students

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

Other table of 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  |
+----+--------+-------+

Finally i want result like

+----+--------+-------+
| id | name   | status|
+----+--------+-------+
|  1 | Alex   |   1   |
|  2 | Bill   |   1   |
|  3 | Cath   |   1   |
|  4 | Dale   |  NULL |
|  5 | Evan   |  NULL |
+----+--------+-------+

How to fetch records using mysql queries? I try below query but isn't work..

SELECT students.id, students.name, IF(COALESCE(ex_students.id,1) = 1,'1','2') as status FROM students LEFT JOIN ex_students on FIND_IN_SET(ex_students.s_ids,students.id) > 0 Group By students.id

Change

FIND_IN_SET(ex_students.s_ids, students.id)

to

FIND_IN_SET(students.id, ex_students.s_ids)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM