Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql FIND_IN_SET() with semicolons instead of comma's

Tags:

php

mysql

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

like image 366
user2636556 Avatar asked Dec 29 '14 07:12

user2636556


1 Answers

You can replace the commata:

FIND_IN_SET(b.id ,REPLACE(a.forDepts, ';', ',') )
like image 168
bish Avatar answered Oct 02 '22 13:10

bish