Mysql Nicknames in Inner Join

Asked

Viewed 3,065 times

9

I tried to do this internal merge in SQL to connect two tables in my report.

What I’d be doing wrong, why is giving "ffccf column unknown", even though I nicknamed the column?

Follows the code:

SELECT finafim.ccf as ffccf, finafim.impcaixa, finafim.numcup, finafim.vlfina, finafim.descfina,
       tabc470.ccf as t4ccf, tabc470.NSerie, tabc470.numcupom
FROM finafim
INNER JOIN tabc470
ON (finafim.numcup = tabc470.numcupom)
AND (finafim.ccf = tabc470.ccf)
AND (finafim.impcaixa = tabc470.NSerie)
WHERE ffccf= :ppccf AND numcup= :ppcoo AND impcaixa= :ppecf //Detalhes de parâmetros para igualar valores.
AND dtcomp BETWEEN "2014/03/01" AND "2014/05/01"

2 answers

9

You can only use column references for your apelidos (alias) in the following clauses:

  • GROUP BY
  • ORDER BY
  • HAVING

(Mysql documentation)

So in your case your query would have to reference the columns directly on WHERE instead of using the apelidos.

  • 1

    Count, sum, avg also

  • 1

    @Enzotiezzi can give apelidos the results of the aggregation functions, but cannot reference columns by their apelidos within the functions.

  • but if you from a select Count(table), you will need to nickname to be able to withdraw the value, in case it is valid then also.

  • @Enzotiezzi doesn’t need to name the count(...) to remove the value (in this case the result would appear Count(...)) (sqlfiddle)

1


SELECT DISTINCT ff.ccf AS ffccf, ff.numcup, ff.impcaixa, t46.ccf AS t46ccf, t46.numcupom, t46.NSerie 
FROM finafim AS ff INNER JOIN tabc470 AS t46 
ON (ff.numcup = t46.numcupom) AND (ff.ccf = t46.ccf) AND (ff.impcaixa = t46.NSerie)
WHERE ff.ccf= :ppccf AND numcup= :ppcoo AND impcaixa= :ppecf //Detalhes de parâmetros para igualar valores. 
AND dtcomp BETWEEN "2014/03/01" AND "2014/05/01"

Follow the correct code... It just points to the column exactly as Omni said, under those three conditions. I was pointing him inside the Where, where it’s not viable, nor allowed.

Excuse me the lack of organization, is that my company has blocked several forum sites, and I’m not managing to organize my post right.

  • In fact I managed to solve the problem with this, I just removed a few columns that I didn’t need. And solved the problem, meaning I just selected what I would use.

Browser other questions tagged

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