From according to the documentation , MySQL does not allow the use of subqueries in FROM
when the query is a view
:
Subqueries can not be used in the FROM clause of a view.
That results in the following error:
1349: View's SELECT contains a subquery in the FROM clause
The following query returns the sum of the count of the records of each table:
SELECT SUM(total)
FROM (
SELECT COUNT(*) AS total
FROM tabela1
WHERE escondido='não'
UNION ALL
SELECT COUNT(*) AS total
FROM tabela2
WHERE escondido='não'
) t
Assuming 1000 records in tabela1
and 500 in tabela2
, query returns 1500.
With the following change to the query, it is already possible to create view
, but the result is two lines, each with the total records of each table:
SELECT COUNT(*) AS total
FROM tabela1
WHERE escondido='não'
UNION ALL
SELECT COUNT(*) AS total
FROM tabela2
WHERE escondido='não'