vendredi 3 août 2018

How to compare comma separated ids on left join

I want to get the count of cases which are there for other tale in a format of comma separated

I have a table like below

Table1

id        name
1         a
2         b
3         c
4         d
5         e
6         f

Table2

id       table1_ids   user_id
1        1,2,3,4,5    1
2        1,2,3        2
3        1,2,3,4,5    1
4        1,2,3,4      2

When i join them, i want to display the count of table_ids in table2 like below

 Expected:        a-4  b-4  c-4  d-3  e-5  f-0
 Getting output:  a-4  b-4  c-4

I have tried query like below using laravel raw query

DB::select('select t1.name, t1.id, count(t2.id) as count from table1 as t1 left join table2 as t2 on FIND_IN_SET(t1.id, t2.table1_ids)>0  where t2.user_id in ('1,2') group By t1.name, t1.id');

Please suggest me how can i acheive this



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire