Mysql使用一对多关系加入非匹配表

时间:2013-06-06 18:09:28

标签: php mysql join

我试图从表1(报告)中获取所有结果,并将其他两个表连接到它(用户)和(工作单位)

报告包含与用户和工作人员相关的密钥,但它们存储在csv值中。 我试图执行类似于这个伪代码的东西

`SELECT * 
FROM reports 
LEFT JOIN users ON reports = (WHERE users.userID IN (reports.users))
LEFT JOIN workorders ON reports = (WHERE workorder.status IN (reports.filters) 
AND reports.reportid = 10
`

reports.users和reports.filters看起来像“1,2,3,4,5,6”

1 个答案:

答案 0 :(得分:1)

如果我理解正确(意味着reports.usersreports.filters是逗号分隔值的字符串),则需要FIND_IN_SET函数:

SELECT * from reports 
LEFT JOIN users ON FIND_IN_SET(users.userID, reports.users)
LEFT JOIN workorders ON FIND_IN_SET(workorder.status, reports.filters) 
  AND reports.reportid = 10