2 calls to the same table in Sqlite (output different than expected)

Asked

Viewed 45 times

0

Given the following 2 tables:

CREATE TABLE IF NOT EXISTS sugestoes (
    id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    texto  VARCHAR(250),
    autor  VARCHAR(250),
    itens  VARCHAR(250),
    pontos INTEGER
);

CREATE TABLE IF NOT EXISTS vertices (
    id            INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    id_node_pai   INTEGER,
    id_node_filho INTEGER,
    FOREIGN KEY (id_node_pai)   REFERENCES sugestoes(id),
    FOREIGN KEY (id_node_filho) REFERENCES sugestoes(id)
);

tabela sugestoes

tabela vertices

resultado


I’m doing the following consultation:

SELECT DISTINCT s.autor
FROM sugestoes AS s
LEFT JOIN vertices AS v
WHERE s.id <> v.id_node_pai; 

In order to get the table results sugestoes not on the table vertices. When I use the sign = it does the correct listing, showing only the same results (author 2 and author 4 of the example), but when I switch to the sign of <>, the result I have is all the names of authors in the table.

I do not know if the flaw is in my logic or in the way I am preparing the consultation.

  • 1

    the correct syntax of Join would be LEFT JOIN vertices AS v&#xA;ON s.id = v.id_node_pai

  • @Ricardopunctual, I don’t really know the difference between WHERE and ON, But in my example the way out was the same, another point, I don’t want the same values, I want the different ones, when I look for the same, sure, but when I look for the different ones he lists them all, regardless of whether they are the same or different.

1 answer

1


If your database is Mysql, try the following:

SELECT autor
FROM sugestoes

MINUS

SELECT s.autor
FROM sugestoes AS s
INNER JOIN vertices AS v ON v.id_node_pai = s.id
  • I am using Sqlite, it is a small application to test, I tried to use this query and it did not work, so I did a search, from what I could see, Sqlite does not have the command MINUS, but it is recommended to use the EXCEPT in place and worked.

Browser other questions tagged

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