Got a tables:
CREATE TABLE users (
    id serial NOT NULL,
    firstname varchar(64) NOT NULL,
    lastname varchar(64) NOT NULL,
    kid integer NOT NULL
);
CREATE TABLE keys (
    id serial NOT NULL,
    key varchar(1024) NOT NULL,
    tid integer NOT NULL
);
CREATE TABLE terminals (
    id serial NOT NULL,
    name varchar(64) NULL,
    ipaddr integer NOT NULL
);
Got a data:
INSERT INTO users (firstname, lastname, kid) VALUES ('jas', 'wedrowniczek', 1);
INSERT INTO users (firstname, lastname, kid) VALUES ('mariusz', 'kolano', 2);
INSERT INTO users (firstname, lastname, kid) VALUES ('ziobro', 'zbigniew', 3);
INSERT INTO users (firstname, lastname, kid) VALUES ('kornel', 'makuszynski', 4);
INSERT INTO users (firstname, lastname, kid) VALUES ('henryk', 'sienkiewicz', 5);
INSERT INTO keys (key, tid) VALUES ('key1', 1);
INSERT INTO keys (key, tid) VALUES ('key2', 2);
INSERT INTO keys (key, tid) VALUES ('key3', 3);
INSERT INTO keys (key, tid) VALUES ('key4', 4);
INSERT INTO keys (key, tid) VALUES ('key4', 5);
INSERT INTO terminals (name, ipaddr) VALUES ('pokoj1', 180879367);
INSERT INTO terminals (name, ipaddr) VALUES ('pokoj2', 180879468);
INSERT INTO terminals (name, ipaddr) VALUES ('pokoj3', 288704395);
INSERT INTO terminals (name, ipaddr) VALUES (NULL, 288703396);
INSERT INTO terminals (name, ipaddr) VALUES ('pokoj5', 0);
I need to get all the logins which have keys to terminal without descriptions or ipaddresses.
The results should be:
 id | firstname |   lastname   |  ipaddr   | name    |
----+-----------+--------------+-----------+----------
  4 | kornel    | makuszynski  | 288703396 |         |
  5 | henryk    | sienkiewicz  | 0         | pokoj5  |
My try:
SELECT u.id, u.firstname, u.lastname, t.ipaddr, t.name
FROM users u
LEFT JOIN keys k ON (u.kid = k.id)
LEFT JOIN terminals t ON (t.id = k.tid)
WHERE
    t.ipaddr = 0
    OR t.name IS NULL;
It gives me empty result.
I understand that: first is going SELECT then filtering is done using WHERE then LEFT JOIN.
...but is it possible to add some filtering at the end using data from LEFT JOIN?
 
     
    