简体   繁体   中英

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)

Reports has keys relating to Users and Workorders but they are stored in csv values. 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"

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:

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

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