Usage of AND vs WHERE clause in SQL

前端 未结 3 1684
囚心锁ツ
囚心锁ツ 2021-01-06 22:39

I am working on PHP page that connects with Oracle. I came across this SQL and I am not sure it is doing what is supposed to, so I thought I would

3条回答
  •  花落未央
    2021-01-06 23:06

    Am I right?

    No. This query:

    select tableA.id, tableA.name, tableB.details 
    from tableA
    left join tableB
    on tableB.id = tableA.id
    and tableB.logId = ''
    

    Is very different from:

    select tableA.id, tableA.name, tableB.details 
    from tableA
    left join tableB
    on tableB.id = tableA.id
    where tableB.logId = ''
    

    It's the criteria for joining.

    In the first case, you take from A, then join with B when there's a matching id and a logId, else leave details null.

    In the second, you take from A, then join with B when there's a matching id, else leave details null, and then you keep only rows from A where there's a B match that has a logId -- eliminating rows from A in the process, and de facto turning it into an inner join.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题