gpt4 book ai didi

sql - 如何在 Oracle 10g 中执行 "selective where"?

转载 作者:行者123 更新时间:2023-12-04 05:39:06 26 4
gpt4 key购买 nike

我有一个父表 A,它有 2 个外键(来自表 B 和 C),但它一次只能有一个外键。示例:

SELECT a.evi, a.time, a.date, 
a.number, a.descr, a.x,
a.y, a.z, a.FK_tableB, a.FK_tableC,
b.brand, b.type,
b.color, b.model, c.name, c.lastname,
FROM tableA a,
tableB b,
tableC c
WHERE (PK_tableA = 100 AND PK_tableB = FK_tableB)
OR (PK_tableA = 100 AND PK_tableC = FK_tableC)

(这显然行不通)

当 where 子句之一为真时,如何返回数据。

最佳答案

您似乎想对查询执行“异或”(XOR)。

由于 SQL 没有 XOR,您可以尝试以下操作:

create table a
( a_id int, b_id int, c_id int);

create table b
( b_id int);

create table c
( c_id int);

insert into a (a_id, b_id, c_id) values (1, 1, 1);
insert into a (a_id, b_id, c_id) values (2, NULL, 2);
insert into a (a_id, b_id, c_id) values (3, 2, NULL);
insert into a (a_id, b_id, c_id) values (4, NULL, NULL);

insert into b (b_id) values (1);
insert into b (b_id) values (2);

insert into c (c_id) values (1);
insert into c (c_id) values (2);

SELECT a.a_id, a.b_id, a.c_id, b.b_id, c.c_id
FROM a
LEFT JOIN b
ON (a.b_id = b.b_id)
LEFT JOIN c
ON (a.c_id = c.c_id)
WHERE ( (b.b_id is NOT NULL AND c.c_id is NULL)
OR (c.c_id is NOT NULL AND b.b_id is NULL));

看到这个 SQLFiddle试试看。

关于sql - 如何在 Oracle 10g 中执行 "selective where"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11505964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com