简体   繁体   中英

MySQL Workbench 1146 error with table alias

I have this code of MySQL in workbench

1. SELECT `ΟΝΟΜΑΣΙΑ_ΠΡΟΪΟΝΤΟΣ`, `ΠΩΛΗΣΗ`
2. FROM(SELECT `ΟΝΟΜΑΣΙΑ_ΠΡΟΪΟΝΤΟΣ`, SUM(`ΠΟΣΟΤΗΤΑ`) AS `ΠΩΛΗΣΕΙΣ`
3. FROM `hospital`.`προιοντα`, `hospital`.`χρεωσεις_περιστατικων`
4. WHERE `hospital`.`προιοντα`.`ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ` = `hospital`.`χρεωσεις_περιστατικων`.`ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ` AND YEAR(`ΗΜ_ΝΙΑ_ΧΡΕΩΣΗΣ`) = 2013
5. GROUP BY `ΟΝΟΜΑΣΙΑ_ΠΡΟΪΟΝΤΟΣ`) AS `Π`
6. WHERE `ΠΩΛΗΣΗ` = (SELECT MAX(`ΠΩΛΗΣΕΙΣ`) FROM `Π`.`ΠΩΛΗΣΕΙΣ`);

The problem exists in line 6. MySQL Workbench doesn't recognize table alias "Π", so it throws me the error:

Error Code: 1146. Table 'π.πωλησεισ' doesn't exist.

What can I do?

That's because Π is a derived table and not a permanent table and so you can't use it in WHERE clause like a normal table. rather include the MAX() calculation in outer query. Change your query like below

  SELECT `ΟΝΟΜΑΣΙΑ_ΠΡΟΪΟΝΤΟΣ`, `ΠΩΛΗΣΗ`
   FROM
    (
    SELECT `ΟΝΟΜΑΣΙΑ_ΠΡΟΪΟΝΤΟΣ`, 
    `ΠΩΛΗΣΗ`,
    SUM(`ΠΟΣΟΤΗΤΑ`) AS `ΠΩΛΗΣΕΙΣ`
     FROM `hospital`.`προιοντα`, `hospital`.`χρεωσεις_περιστατικων`
     WHERE `hospital`.`προιοντα`.`ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ` = 
    `hospital`.`χρεωσεις_περιστατικων`.`ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ` 
     AND YEAR(`ΗΜ_ΝΙΑ_ΧΡΕΩΣΗΣ`) = 2013
     GROUP BY `ΟΝΟΜΑΣΙΑ_ΠΡΟΪΟΝΤΟΣ`
     HAVING `ΠΩΛΗΣΗ` = MAX(`ΠΩΛΗΣΕΙΣ`)
    ) AS `Π`;

PS: BTW, I didn't knew MySQL works in Alien Language as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM