简体   繁体   English

Mysql连接具有一对多关系的不匹配表

[英]Mysql Join non matching tables with one to many relationship

I am trying to get all results from table 1 (Reports) and join the other two tables onto it (users) and (workorders) 我正在尝试从表1(报告)中获取所有结果,并将其他两个表(用户)和(工作订单)加入其中

Reports has keys relating to Users and Workorders but they are stored in csv values. 报表具有与用户和工作单相关的键,但是它们存储在csv值中。 I am trying to peform something similar to this psuedo code 我正在尝试执行类似于此伪代码的操作

`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 and reports.filters look like "1,2,3,4,5,6" report.users和report.filters看起来像“ 1,2,3,4,5,6”

If I understand correctly (meaning that reports.users and reports.filters are strings of comma-delimited values), you need the FIND_IN_SET function for this: 如果我理解正确的话(这意味着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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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