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

标签 sql oracle

我有一个父表 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)

(显然,这不起作用)

当其中一个子句为 true 时,如何返回数据。

最佳答案

您似乎想要对查询执行“异或”(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/

相关文章:

sql - 根据具有名称列名称的另一个表更新表中 100 列行的 SQL

sql - 如何设置存储过程参数可以或不可以由用户传递值?

sql - 在 JOIN 中选择最新记录时出现问题

oracle - 修改多个oracle触发器

sql - Oracle SQL - 我可以返回列值的 "before"状态吗

sql - 如何通过在配置单元的分区表中选择另一列来覆盖列值

sql - Azure 中具有聚集索引的表出现聚集索引错误

sql - 在SQL中将列转换为行

oracle - 为什么截断表不会更改上次 DDL 日期?

sql - 如何修改 SQL 中的默认值以接受 Y 或 N 而不仅仅是 Y