Unknow table in field list when performing select + sum

Asked

Viewed 18 times

1

I’m trying to sum up the values of several different tables, tried two ways:

Without citing the name of the database:

SELECT SUM(animais_adocao.animal_pendente + animais_encontrados.animal_pendente + animais_perdidos.animal_pendente);

And also citing the name of the database:

SELECT SUM(cademeupet.animais_adocao.animal_pendente + cademeupet.animais_encontrados.animal_pendente + cademeuppet.animais_perdidos.animal_pendente)

In both cases it is returned:

Unknow table in field list, as you can see in the image, the column has this name and the bank too, I’m forgetting something?

inserir a descrição da imagem aqui

  • sum does not sum the columns, sum the rows, if you want to add columns use only the +

1 answer

0

To be able to sum up you need to identify the source tables of each field you are using.


In this case it would be something like this (without knowing the structure of the two tables but assuming that they all have a field ID identifying the animal):

SELECT      SUM(aa.animal_pendente + ae.animal_pendente + ap.animal_pendente) AS soma
FROM        animais_adocao      aa
LEFT JOIN   animais_encontrados ae ON ae.id_animal = aa.id_animal
LEFT JOIN   animais_perdidos    ap ON ap.id_animal = aa.id_animal

Browser other questions tagged

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