1)学生桌
student_id student_name
100 yash
101 rahul
102 jay
103 roy
2) stop_table
stop student_id
abc 100,102
def 101,103
这是我的要求
SELECT
student_table.student_name,
stop_table.stop
FROM student_table
JOIN stop_table ON
FIND_IN_SET(student_table.student_id,'101,102')
它并没有停止特定的student_id
result =>
rahul abc
rahul def
jay abc
jay def
答案 0 :(得分:1)
您的JOIN
条件没有任何意义,因为它不会限制stop_table
中的值相对于student_table
中的值的选择,因此,您实际上得到了{{ 1}}在CROSS JOIN
和student_table
之间。这就是为什么每个学生获得多行的原因。您真正需要的是在
stop_table
JOIN
并将您的FIND_IN_SET(student_table.student_id, stop_table.student_id)
条件作为JOIN
条件,以仅选择ID为101和102的学生:
WHERE
输出:
SELECT s.student_name, t.stop
FROM student_table s
JOIN stop_table t ON FIND_IN_SET(s.student_id, t.student_id)
WHERE s.student_id IN (101, 102)
请注意,我对student_name stop
rahul def
jay abc
子句的编写略有不同,但可以写为
WHERE
答案 1 :(得分:0)
SELECT
student_table.student_name,
stop_table.stop
FROM student_table
JOIN stop_table ON
FIND_IN_SET(student_table.student_id, stop_table.student_id)