Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用CTE成功地实现了一个连接(见下文)。但如果实际数据存在,我仍然需要返回数据:)我喜欢您的解决方案;我成功地使用CTE进行了一次连接(见下文)。 create table a(a_id integer not null primary key); cr_Sql_Postgresql - Fatal编程技术网

Sql 使用CTE成功地实现了一个连接(见下文)。但如果实际数据存在,我仍然需要返回数据:)我喜欢您的解决方案;我成功地使用CTE进行了一次连接(见下文)。 create table a(a_id integer not null primary key); cr

Sql 使用CTE成功地实现了一个连接(见下文)。但如果实际数据存在,我仍然需要返回数据:)我喜欢您的解决方案;我成功地使用CTE进行了一次连接(见下文)。 create table a(a_id integer not null primary key); cr,sql,postgresql,Sql,Postgresql,使用CTE成功地实现了一个连接(见下文)。但如果实际数据存在,我仍然需要返回数据:)我喜欢您的解决方案;我成功地使用CTE进行了一次连接(见下文)。 create table a(a_id integer not null primary key); create table b(b_id integer not null primary key); create table c(a_id integer not null references a(a_id) , b_id i


使用CTE成功地实现了一个连接(见下文)。但如果实际数据存在,我仍然需要返回数据:)我喜欢您的解决方案;我成功地使用CTE进行了一次连接(见下文)。
create table a(a_id integer not null primary key);
create table b(b_id integer not null primary key);

create table c(a_id integer not null references a(a_id)
        , b_id integer not null references b(b_id)
        , primary key (a_id,b_id)
        );

insert into a(a_id) values(0),(2),(4),(6);
insert into b(b_id) values(0),(3),(6);
insert into c(a_id,b_id) values(6,6);

PREPARE omg(integer,integer) AS
SELECT    EXISTS(SELECT * FROM a where a.a_id = $1) AS a_exists
        , EXISTS(SELECT * FROM b where b.b_id = $2) AS b_exists
        , EXISTS(SELECT * FROM c where c.a_id = $1 and c.b_id = $2) AS c_exists
        ;
EXECUTE omg(1,1);
EXECUTE omg(2,1);
EXECUTE omg(1,3);
EXECUTE omg(6,6);
PREPARE omg2(integer,integer) AS
SELECT val.a_id AS va_id
        , val.b_id AS vb_id
        , EXISTS(SELECT * FROM a WHERE a.a_id = $1) AS a_exists
        , EXISTS(SELECT * FROM b WHERE b.b_id = $2) AS b_exists
        , EXISTS(select * FROM c WHERE c.ca_id = val.a_id AND c.cb_id = val.b_id ) AS c_exists
        , a.*
        , b.*
        , c.*
FROM (values ($1,$2)) val(a_id,b_id)
LEFT JOIN a ON a.a_id = val.a_id
LEFT JOIN b ON b.b_id = val.b_id
LEFT JOIN c ON c.ca_id = val.a_id AND c.cb_id = val.b_id
        ;
EXECUTE omg2(1,1);
EXECUTE omg2(2,1);
EXECUTE omg2(1,3);
EXECUTE omg2(6,6);
select a_id, b_id , 'case 1' from c 
where not exists (select 1 from a where a.a_id=c.a_id)

union all 

select a_id, b_id ,'case 2' from c 
where not exists (select 1 from b where b.b_id=c.b_id)

union all 

select a_id, b_id, 'case 3' from a cross join b
where exists (select 1 from c where c.a_id=a.a_id)
and exists (select 1 from c where c.b_id=b.b_id)
and not exists (select 1 from c where c.b_id=b.b_id and c.a_id=a.a_id)
CREATE TABLE people 
(
  id integer not null primary key, 
  name text not null
);

CREATE TABLE thing_types 
(
  id integer not null primary key, 
  name text not null
);

CREATE TABLE things
(
  id integer not null primary key, 
  person_id integer not null references people(id), 
  thing_type_id integer not null references thing_types(id), 
  name text not null
);

INSERT INTO people VALUES (1, 'Bill');
INSERT INTO thing_types VALUES (1, 'game');

INSERT INTO things VALUES (1, 1, 1, 'Duke Nukem');
INSERT INTO things VALUES (2, 1, 1, 'Warcraft 2');
WITH v AS (
  SELECT (SELECT id FROM people WHERE id=<person_id_param>) AS person_id, 
         (SELECT id FROM thing_types WHERE id=<thing_type_param>) AS thing_type_id
)
SELECT v.person_id, v.thing_type_id, things.name 
FROM 
  v LEFT JOIN things 
    ON v.person_id = things.person_id AND v.thing_type_id = things.thing_type_id
person_id  thing_type_id  name
-------------------------------------
        1              1   Duke Nukem
        1              1   Warcraft 2
person_id  thing_type_id  name
-------------------------------------
     NULL              1         NULL
person_id  thing_type_id  name
-------------------------------------
        1              1         NULL