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