Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>well you could try something along these lines:</p> <p>this is test setup:</p> <pre><code>mysql&gt; select * from table1; +------+------------+ | id | permission | +------+------------+ | a1 | 1, 2, 3, 4 | | v2 | 2, 3, 4 | +------+------------+ 2 rows in set (0.01 sec) mysql&gt; select * from table2; +------+------------+ | id | permission | +------+------------+ | 1 | Allow | | 2 | Not Allow | | 3 | Disabled | +------+------------+ 3 rows in set (0.01 sec) mysql&gt; select id, GROUP_CONCAT(t2perm) from (SELECT t1.*, t2.id as t2id, t2.permission as t2perm from table2 t2 cross join table1 t1) crs where INSTR(permission, t2id) &gt; 0 group by id; +------+--------------------------+ | id | GROUP_CONCAT(t2perm) | +------+--------------------------+ | a1 | Allow,Not Allow,Disabled | | v2 | Not Allow,Disabled | +------+--------------------------+ 2 rows in set (0.00 sec) </code></pre> <p>To explain a bit; first you cross join both tables and that should result in cartesian product, like so:</p> <pre><code>mysql&gt; SELECT * from table2 cross join table1; +------+------------+------+------------+ | id | permission | id | permission | +------+------------+------+------------+ | 1 | Allow | a1 | 1, 2, 3, 4 | | 1 | Allow | v2 | 2, 3, 4 | | 2 | Not Allow | a1 | 1, 2, 3, 4 | | 2 | Not Allow | v2 | 2, 3, 4 | | 3 | Disabled | a1 | 1, 2, 3, 4 | | 3 | Disabled | v2 | 2, 3, 4 | +------+------------+------+------------+ 6 rows in set (0.00 sec) </code></pre> <p>from that point, just select rows that have one string contained in another (INSTR(permission, t2id) => mapping permissions onto ids), you'll end up with this:</p> <pre><code>mysql&gt; select * from (SELECT t1.*, t2.id as t2id, t2.permission as t2perm from table2 t2 cross join table1 t1) crs where INSTR(permission, t2id) &gt; 0; +------+------------+------+-----------+ | id | permission | t2id | t2perm | +------+------------+------+-----------+ | a1 | 1, 2, 3, 4 | 1 | Allow | | a1 | 1, 2, 3, 4 | 2 | Not Allow | | v2 | 2, 3, 4 | 2 | Not Allow | | a1 | 1, 2, 3, 4 | 3 | Disabled | | v2 | 2, 3, 4 | 3 | Disabled | +------+------------+------+-----------+ 5 rows in set (0.00 sec) </code></pre> <p>now just aggregate results with GROUP_CONCAT...</p> <pre><code>select id, GROUP_CONCAT(t2perm) from (SELECT t1.*, t2.id as t2id, t2.permission as t2perm from table2 t2 cross join table1 t1) crs where INSTR(permission, t2id) &gt; 0 group by id; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload