Add multiple select Count results to MYSQL

Asked

Viewed 3,116 times

2

I’m wanting to add up all the clients I have on all my bases, so for that I’m doing the following query:

SET @total = 0;

USE BASE x1; SELECT @total:= @total +COUNT(1) FROM clientes;

USE BASE x2; SELECT @total:= @total +COUNT(1) FROM clientes;

in the current scenario my variable is taking the value of itself and adding to the value of COUNT(1);

But every query executed, a result is displayed, I would like it to be displayed only the final result!

  • A simple SELECT COUNT(*) FROM clientes already will not return you your total customers? Or you want to do this with variables even?

1 answer

4


Can add up the result of a Subselect

SELECT 
     SUM(CLIENTES) 
FROM (
        SELECT 
            COUNT(1) AS CLIENTES FROM X1.CLIENTES
    UNION ALL
        SELECT 
            COUNT(1) AS CLIENTES FROM X2.CLIENTES
     )  AS T
  • SQL Error (1248): Every derived table must have its Own alias, after closing the parentheses of FROM I had to put an 'as total'... ai worked, I edited your answer!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.