2
respostas

select a.nome from aluno a where not exists

Caros , Estou com o seguinte problema :

mysql> select a.nome from aluno a where not exists (select m.id from matricula m where m.aluno_id = a.id and m.data < now () - interval 18 month);
ERROR 1630 (42000): FUNCTION sql2.now does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual

Código completo:

mysql> desc matricula;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| aluno_id | int(11)          | NO   |     | NULL    |                |
| curso_id | int(11)          | NO   |     | NULL    |                |
| data     | datetime         | NO   |     | NULL    |                |
| tipo     | varchar(20)      | NO   |     |         |                |
+----------+------------------+------+-----+---------+----------------+
5 rows in set (0.03 sec)

mysql> desc curso;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| nome  | varchar(255)     | NO   |     |         |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.05 sec)

mysql> desc matricula;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| aluno_id | int(11)          | NO   |     | NULL    |                |
| curso_id | int(11)          | NO   |     | NULL    |                |
| data     | datetime         | NO   |     | NULL    |                |
| tipo     | varchar(20)      | NO   |     |         |                |
+----------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> select a.nome, c.nome from aluno a join
    -> matricula m on m.aluno_id = a.id.join
    -> curso c on m.curso_id = c.id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'curso c on m.curso_id = c.id' at line 3
mysql> select a.nome, c.nome from aluno a join
    -> matricula m on m.aluno_id = a.id.join
    -> curso c on m.curso_id = c.id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'curso c on m.curso_id = c.id' at line 3
mysql> select a.nome, c.nome from aluno a join matricula m on m.aluno_id = a.id join curso c on m.curso_id = c.id;
+-----------------+---------------------------------+
| nome            | nome                            |
+-----------------+---------------------------------+
| João da Silva   | SQL e banco de dados            |
| Frederico José  | SQL e banco de dados            |
| Alberto Santos  | Scrum e métodos ágeis           |
| Renata Alonso   | PHP e MySql                     |
| Frederico José  | Desenvolvimento web com VRaptor |
+-----------------+---------------------------------+
5 rows in set (0.00 sec)

mysql> select a.nome from aluno a where exists (select m.id from matricula m where m.aluno_id = a.id);
+-----------------+
| nome            |
+-----------------+
| João da Silva   |
| Frederico José  |
| Alberto Santos  |
| Renata Alonso   |
+-----------------+
4 rows in set (0.06 sec)

mysql> select a.nome from aluno a where not exists (select m.id from matricula m where m.aluno_id = a.id);
+----------------+
| nome           |
+----------------+
| Paulo da Silva |
+----------------+
1 row in set (0.05 sec)

mysql> select a.nome from aluno a where not exists (select m.id from matricula m where m.aluno_id = a.id)
    -> and
    -> n.data < now () - interval 18 month);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 3
mysql> select a.nome from aluno a where not exists (select m.id from matricula m where m.aluno_id = a.id
    -> and
    -> n.data < now () - interval 18 month);
ERROR 1054 (42S22): Unknown column 'n.data' in 'where clause'
mysql> select a.nome from aluno a where not exists (select m.id from matricula m where m.aluno_id = a.id and n.data < now () - interval 18 month);
ERROR 1054 (42S22): Unknown column 'n.data' in 'where clause'
mysql> select a.nome from aluno a where not exists (select n.id from matricula m where m.aluno_id = a.id and n.data < now () - interval 18 month);
ERROR 1054 (42S22): Unknown column 'n.id' in 'field list'
mysql> select a.nome from aluno a where not exists (select m.id from matricula m where m.aluno_id = a.id and m.data < now () - interval 18 month);
2 respostas

Guilherme

vi no primeiro código que o

now ()

ta separado o parenteses da palavra now, tem que ser junto:

now()

Além disso, no seu último código você esta esquecendo de fechar o parenteses corretamente e você ta usando como alias na tabela matricula m

mas esta usando campos

n.data e n.id

o certo é m.data e m.id

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software