SQL-WHERE AND / OR子句的替代方法

时间:2018-11-30 12:31:08

标签: sql oracle

我是一个初学者,我对此事有基本的了解,因此,如果我找不到正确的答案,我会打个招呼。 这是我编写的代码。它返回的正是我所需要的,但是我觉得这不是一个很好的解决方案,有什么建议吗? 我正在使用Oracle SQL Developer。预先感谢

SELECT
    ta.id,
    tb.taid,
    tc.idb,
    ta.durata,
    tc.durata,
    tb.durata,
    ta.data_attivazione,
    ta.data_disattivazione,
    tc.data_inizio,
    tc.data_fine,
    tb.data_inizio,
    tb.data_fine
FROM
    tableA ta
    tableB ta
    tableC tc
WHERE
    ta.id = 3456
    AND tb.taid = 3456
    AND tc.idb = 3456
    OR (
        ta.id = 3457
        AND tb.taid = 3457
        AND tc.idb = 3457
    )

3 个答案:

答案 0 :(得分:4)

使用正确,明确,标准 JOIN语法:

select . . .
from tableA ta join
     tableB tb
     on ta.id = tb.taid join
     tableC tc
     on tc.idb = ta.id
where ta.id in (3456, 3457)

答案 1 :(得分:3)

使用join

SELECT
    ta.id,
    tb.taid,
    tc.idb,
    ta.durata,
    tc.durata,
    tb.durata,
    ta.data_attivazione,
    ta.data_disattivazione,
    tc.data_inizio,
    tc.data_fine,
    tb.data_inizio,
    tb.data_fine
FROM
    tableA ta join tableB tb on ta.id= tb.taid
    join tableC tc on tb.taid = tc.idb
WHERE
    ta.id in (3456, 3457)

答案 2 :(得分:-1)

您可以使用联接来简化代码:

enter image description here