我正在尝试加入两个表,但似乎遇到了问题,我正在寻找的输出是有主题的行数。
SELECT *
FROM education AS a
JOIN keys2 AS b USING(`List Idsubjek`)
WHERE `List Idsubjek` IN (52, 54, 55, 67)
AND `studentid` = '$id'
keys2表如下所示:
id List Idsubjek Subject
1 52 Maths
2 53 Geography
教育表如下所示:
id List Idsubjek studentid school
1 52,53,54,74,0,0,0 15 school name
我目前只生产一行,额外的列“主题”,但它只生成第一行。数学。
任何帮助都将不胜感激。
答案 0 :(得分:1)
首先,您应该规范化数据结构,并在keys2和教育表之间建立连接表。
要使联接与当前数据结构一起使用,请使用mysql' find_in_set()函数:
SELECT *
FROM education AS a
JOIN keys2 AS b on find_in_set(b.`List Idsubjek`,a.`List Idsubjek`)>0
WHERE b.`List Idsubjek` IN (52, 54, 55, 67)
AND `studentid` = '$id'