Perform join on a table depending on the value of a field

0

I'm trying to make a query in Mysql and according to the column type of my table reports, make a JOIN to one table or another.

Reports:

In type the type of the item to which it is reported is stored, which can be channel or group . According to the type of item that has been reported, I intend to make a JOIN to the table of groups or channels to get the title and other data.

The structure of channels and groups is the same.

So far I have tried to do the following:

SELECT count(*) as contador, type, 'id_type', b.title, IF(STRCMP(report.type,'group'), 'groups', 'channels') as m  FROM 'report' JOIN m as b ON report.id_type = b.id GROUP BY 'id_type', 'type' ORDER BY contador DESC;

But it tells me that the m table does not exist. I also tried to put the IF in this way:

SELECT count(*) as contador, type, 'id_type', b.title FROM 'report' JOIN IF(STRCMP(report.type,'group'), 'groups', 'channels') as b ON report.id_type = b.id GROUP BY 'id_type', 'type' ORDER BY contador DESC;

And he returns me:

  

1064 - Something is wrong in its syntax near 'IF (STRCMP (report.type,' group '),' groups ',' channels ') as b ON report.id_type = b' on line 1

    
asked by AntoTGa 03.02.2017 в 23:24
source

2 answers

1

This type of conditional inner join can be done with 2 left joins and some additional conditions in clause WHERE in this way:

select count(*) as contador,
       r.type,
       r.id_type,
       case when g.id is not null 
            then g.title
            else c.title
       end as title -- aquí aún tienes un problema...
  from report r
  left join groups g
    on g.id = r.id_type
   and r.type = 'group'
  left join channels c
    on c.id = r.id_type
   and r.type = 'channel'
 where g.id is not null
    or c.id is not null
 group by r.id_type,
          r.type
 order by contador

The only problem, as I mentioned, is that you are trying to return a value for the field title without applying an aggregation function, which does not make sense if you do not include the title in the clause GROUP BY . The consequence of this is that there is no way of knowing from which record the value for title will be drawn. That is something that you will have to define and fix. Without having more details regarding your data model, I can not help you anymore.

    
answered by 04.02.2017 / 01:09
source
1

Another way to solve the problem, but as Comrade Sstan says, you have a problem to solve in the title field:

SELECT count(*) as contador,r.id_type, r.type, COALESCE(g.title, c.title) as title  FROM report r
LEFT JOIN groups as g ON r.id_type = g.id
LEFT JOIN channels as c ON r.id_type = c.id 
WHERE (CASE WHEN r.type='group' THEN g.id is not null ELSE c.id is not null END)
GROUP BY r.id_type, r.type
ORDER BY contador DESC;

a possible solution could be to group the title fields and show them separated by ',' for this you should change in the select COALESCE(g.title, c.title) by COALESCE(group_concat(g.title SEPARATOR ','), group_concat(c.title SEPARATOR ',')) as title

    
answered by 04.02.2017 в 17:33