By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE tbl (
id int,
text varchar(500)
);
INSERT INTO tbl VALUES
(1, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'),
(2, 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat');
2 rows affected
SELECT * FROM tbl;
id | text |
---|---|
1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua |
2 | Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat |
-- выбираем существующую запись
SELECT pt.pid, t.id, t.text FROM (SELECT 1 AS pid) pt
FULL OUTER JOIN (
SELECT 1 AS pid, tt.*
FROM tbl tt
WHERE tt.id = 1
) AS t ON pt.pid = t.pid
;
pid | id | text |
---|---|---|
1 | 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua |
-- выбираем несуществующую запись
SELECT pt.pid, t.id, t.text FROM (SELECT 1 AS pid) pt
LEFT OUTER JOIN (
SELECT 1 AS pid, tt.*
FROM tbl tt
WHERE tt.id = 10
) AS t ON pt.pid = t.pid
;
pid | id | text |
---|---|---|
1 | null | null |
SELECT id, text FROM tbl
WHERE id = 10
UNION
SELECT NULL, NULL
;
id | text |
---|---|
null | null |