MySQL Query - Count different table fields

1

I need to create a view in MySQL where I get the following result.

Col1 | Col2 Total Tab1 | Total Tab2

I'm using UNION as follows, but the result comes as follows:

col1 Page 201 Page 5699

CREATE VIEW 'dashboards' AS (
  select 
    count('clientes'.'id') AS 'col1' 
  from 
    'clientes') union all (
  select 
    count('titulos'.'id') AS 'col2' 
  from 
    'titulos');

But I need it to return as columns and not as lines.

Can anyone help me?

    
asked by anonymous 16.11.2015 / 14:01

2 answers

2

I usually do this:

select 
    count(clientes.id) as coluna1, count(titulos.id) as coluna2, sum(titulos.valor) as total
from
    clientes, titulos

Return something like this:

coluna1 | coluna2 | total
  10    |    25   |   50

If you need more columns with some formula call in the select added comma to separate.

    
16.11.2015 / 14:20
1

Explanation

You can develop your query by making a subquery a field. Use as an example the example below.

Example

  CREATE VIEW 'dashboards' AS (
      SELECT (
      select 
        count('clientes'.'id') 
      from 
        'clientes') AS 'col1',(
      select 
        count('titulos'.'id')  
      from 
        'titulos') AS 'col2';
    
16.11.2015 / 14:18