Вы можете попытаться использовать JOIN
с условной функцией агрегирования
create table Table1(
userID int,
Name varchar(50)
);
insert into Table1 values (1,'aaa');
insert into Table1 values (2,'bbb');
insert into Table1 values (3,'ccc');
create table Table2(
functionID int,
userID int
);
insert into Table2 values (1,1);
insert into Table2 values (2,1);
insert into Table2 values (2,2);
insert into Table2 values (3,2);
insert into Table2 values (1,3);
insert into Table2 values (3,3);
Запрос 1 :
SELECT t1.userID,
t1.Name,
IFNULL(MAX(CASE WHEN t2.functionID = 1 then 'true' end),'false') function1Enabled,
IFNULL(MAX(CASE WHEN t2.functionID = 2 then 'true' end),'false') function2Enabled,
IFNULL(MAX(CASE WHEN t2.functionID = 3 then 'true' end),'false') function3Enabled
FROM
Table1 T1
LEFT JOIN Table2 t2 ON t2.userID = T1.userID
GROUP BY t1.userID,
t1.Name
Результаты :
| userID | Name | function1Enabled | function2Enabled | function3Enabled |
|--------|------|------------------|------------------|------------------|
| 1 | aaa | true | true | false |
| 2 | bbb | false | true | true |
| 3 | ccc | true | false | true |