Murilo Pereira
Murilo Pereira

Reputation: 109

INNER JOIN doesn't return any result - SQLite

I'm trying to consult my database and return values from a table what have Foregin Key with another three tables, but when I use INNER JOIN, WHERE and AND in the same Query, I recive nothing.

My main table:

CREATE TABLE Matematica (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, 
    Pergunta TEXT NOT NULL, 
    Alternativas TEXT, 
    Gabarito TEXT NOT NULL, 
    Assunto INTEGER NOT NULL REFERENCES Assunto (ID), 
    Vestibular INTEGER NOT NULL REFERENCES Universidades (ID), 
    Ano NUMERIC NOT NULL, 
    Tipo INTEGER NOT NULL, 
    Imagem TEXT, Previa TEXT NOT NULL, 
    OBS TEXT, 
    Fonte TEXT, 
    qImagem TEXT);
+----+--------------------+--------------+----------+---------+------------+------+------+--------+----------+-----+-------+---------+
| ID |      Pergunta      | Alternativas | Gabarito | Assunto | Vestibular | Ano  | Tipo | Imagem |  Previa  | OBS | Fonte | qImagem |
+----+--------------------+--------------+----------+---------+------------+------+------+--------+----------+-----+-------+---------+
|  1 | SOME BIG QUESTION  | a)A          | a        |       2 |          1 | 2014 |    0 |      3 | SOME BIG |     |       |         |
|    |                    | b)B          |          |         |            |      |      |        | TEXT     |     |       |         |
|    |                    | c)C          |          |         |            |      |      |        |          |     |       |         |
|    |                    | d)D          |          |         |            |      |      |        |          |     |       |         |
+----+--------------------+--------------+----------+---------+------------+------+------+--------+----------+-----+-------+---------+

Assunto table:

CREATE TABLE Assunto (ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 
    Assunto TEXT NOT NULL, 
    Disciplina TEXT NOT NULL);
+----+--------------------------+------------+
| ID |         Assunto          | Disciplina |
+----+--------------------------+------------+
|  1 | DNA e RNA                | Biologia   |
|  2 | Geometria plana          | Matemática |
|  3 | Evolução                 | Biologia   |
|  4 | Associação de resistores | Física     |
|  5 | Realismo e Naturalismo   | Literatura |
+----+--------------------------+------------+

Universidades table:

CREATE TABLE Universidades (ID INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT NOT NULL, 
    Vestibular TEXT (50) NOT NULL);
+-----+------------+
| ID  | Vestibular |
+-----+------------+
|   1 | FUVEST     |
|   2 | UNICAMP    |
|   3 | VUNESP     |
+-----+------------+

My SQL Query:

SELECT Pergunta,
       Alternativas,
       Gabarito,
       Assunto.Assunto          AS Assunto,
       Universidades.Vestibular AS Vestibular,
       Ano,
       Tipo,
       Imagem,
       Previa,
       OBS,
       Fonte,
       qImagem,
       Matematica.ID
FROM   Matematica
       INNER JOIN Assunto
               ON Assunto.ID = Matematica.Assunto
       INNER JOIN Universidades
               ON Universidades.ID = Matematica.Vestibular
WHERE  Ano = 2014
       AND Matematica.Vestibular = 'FUVEST' 

If I change from 'FUVEST' to 1, the query returns the row existing in the table.

Thanks.

Upvotes: 1

Views: 143

Answers (1)

MikeT
MikeT

Reputation: 56953

In the following the column Matematica.Vestibular will be numeric as it's the column that references the ID column ( defined as ID INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT NOT NULL) in the Universidades table. hence why 1 worked, 2 and 3 would also have worked (if there were rows in the Matematica table referenceing the UNICAMP and VUNESP i.e. UNICAMP has ID of 2 VUNESP is 3), any other number wouldn't.

SELECT Pergunta,
       Alternativas,
       Gabarito,
       Assunto.Assunto          AS Assunto,
       Universidades.Vestibular AS Vestibular,
       Ano,
       Tipo,
       Imagem,
       Previa,
       OBS,
       Fonte,
       qImagem,
       Matematica.ID
FROM   Matematica
       INNER JOIN Assunto
               ON Assunto.ID = Matematica.Assunto
       INNER JOIN Universidades
               ON Universidades.ID = Matematica.Vestibular
WHERE  Ano = 2014
       AND Matematica.Vestibular = 'FUVEST' 

What I believe you are trying to check is if the the Universidades is a FUVEST as such the above should be changed to (last line changed) :-

SELECT Pergunta,
       Alternativas,
       Gabarito,
       Assunto.Assunto          AS Assunto,
       Universidades.Vestibular AS Vestibular,
       Ano,
       Tipo,
       Imagem,
       Previa,
       OBS,
       Fonte,
       qImagem,
       Matematica.ID
FROM   Matematica
       INNER JOIN Assunto
               ON Assunto.ID = Matematica.Assunto
       INNER JOIN Universidades
               ON Universidades.ID = Matematica.Vestibular
WHERE  Ano = 2014
       AND Universidades.Vestibular = 'FUVEST' 

Upvotes: 1

Related Questions