SELECT id,
(SELECT COUNT(*) from devices where devices.name = call_list.num) "src_count",
IF(src_count = 0, call_list.num, call_list.num2) "client_num",
FROM call_list;
SELECT id,
IF((SELECT COUNT(*) from devices where devices.name = call_list.num) = 0, call_list.num, call_list.num2) "client_num",
FROM call_list;
select
call_list.id,
t.count
from call_list
join (select count(*) as count, devices.name from devices group by devices.name) as t
on t.name = call_list.num;