I have a database set that looks something like this
Table: Notes
nid | forDepts
--------------
1 | 1;2;4
2 | 4;5
Table: Positions
id | name
--------------
1 | Executive
2 | Corp Admin
3 | Sales
4 | Art
5 | Marketing
This query will work if data in the forDepts
was separated by a comma's
SELECT a.nid,
GROUP_CONCAT(b.name ORDER BY b.id) DepartmentName
FROM Notes a
INNER JOIN Positions b
ON FIND_IN_SET(b.id, a.forDepts) > 0
GROUP BY a.nid
is there a way to match it with a semicolon separator? Or is there a better way to do this? Both my tables are pretty big (5336 and 930 rows).
I could do 2 queries, and explode by ;
and match accordingly, but if there is a better way that can be done in single query, that would be great.
Here is my sqlfiddle
You can replace the commata:
FIND_IN_SET(b.id ,REPLACE(a.forDepts, ';', ',') )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With