Когда вы делаете это таким образом, вы создаете декартово произведение для всех этих таблиц, а это очень требовательно для вашего сервера базы данных.
Если вы хотите сделать это в одном запросе, следуйте этому шаблону , что даст вам одну строку для каждой таблицы:
select 'purchase_orders' as table_name, max(created) as last_created, max(modified) as last_modified
from purchase_orders
union all
select 'jobs', max(created), max(modified)
from jobs
union all
select 'companies', max(created), max(modified)
from companies
union all
select 'stock', max(created), max(modified)
from stock
Обычно я бы перевел это на основной язык (PHP, в вашем случае), но если вам нужно сделать это внутри запроса , то должно работать примерно так:
with latest_dates as (
select 'purchase_orders' as table_name, max(created) as last_created, max(modified) as last_modified
from purchase_orders
union all
select 'jobs', max(created), max(modified)
from jobs
union all
select 'companies', max(created), max(modified)
from companies
union all
select 'stock', max(created), max(modified)
from stock
)
select max(last_created) filter (where table_name = 'purchase_order') as last_purchase_order_created,
max(last_modified) filter (where table_name = 'purchase_order') as last_purchase_order_modified,
max(last_created) filter (where table_name = 'job') as last_job_created,
max(last_modified) filter (where table_name = 'job') as last_job_modified,
max(last_created) filter (where table_name = 'company') as last_company_created,
max(last_modified) filter (where table_name = 'company') as last_company_modified,
max(last_created) filter (where table_name = 'stock') as last_stock_created,
max(last_modified) filter (where table_name = 'stock') as last_stock_modified
from latest_dates;