SQL join two tables with specific condition

て烟熏妆下的殇ゞ 提交于 2020-05-15 04:54:27

问题


Table A structure:

enter image description here

Table B structure:

enter image description here

Above are two tables, TableB.TableARelationID is a relationID which used to map table A.

Desired output:

enter image description here

The desired result would be taking TableA.RecordID and TableB.Text, but only of Type 2 in table B, i.e. ignore Type 1

Below is the SQL query which I used:

SELECT tablea.recordid, 
       tableb.text 
FROM   tablea 
       LEFT JOIN tableb 
              ON tablea.relationid = tableb.tablearelationid 
WHERE  type = 2 

But the above query would output:

enter image description here

i.e. RecordID 1 was missing, as the "where" clause filtered.

So how can I show RecordID 1 from Table A?


回答1:


You need to move the type = 2 filter to the join condition:

SELECT  TableA.RecordID, TableB.Text 
FROM    TableA 
        LEFT JOIN TableB 
            ON TableA.RelationID = TableB.TableARelationID 
            AND TableB.Type = 2;

Consider the result of just this:

SELECT  TableA.RecordID, TableB.Text, TableB.Type
FROM    TableA 
        LEFT JOIN TableB 
            ON TableA.RelationID = TableB.TableARelationID;

You would get

RecordID | Text | Type
  1      | NULL | NULL
  2      |   B  |  2
  3      |   C  |  2
  4      |   D  |  2

Then you are filtering on the type column, so for recordID = 1 you have where NULL = 2 which is false (it is not actually false, it is null, but it is not true), so this record is elimitated from the final result.

Whenever you left join you must apply any filtering criteria you wish to apply to the left table in the join condition not the where, otherwise you effectively turn it into an inner join.




回答2:


You should use TableB.Type = 2

  select TableA.RecordID, TableB.Text 
    from    TableA, 
             TableB 
                where TableA.RelationID = TableB.TableARelationID 
                and TableB.Type = 2



回答3:


Add Type = 2 condition in LEFT JOIN conditions.

Try this:

SELECT A.RecordID, B.Text 
FROM TableA A 
LEFT JOIN TableB B ON A.RelationID = B.TableARelationID AND B.Type = 2



回答4:


Try

SELECT tA.RecordID, tB.Text 
FROM [TableA] AS tA
LEFT JOIN [TableB] AS tB
 ON tA.RelationID = tB.TableARelationID 
 AND tB.Type = 2



回答5:


If you filter using the Where statement the join will be treated as a inner join.

select 
   TableA.RecordID
   , TableB.Text 
from 
   TableA 
   left join TableB on TableA.RelationID = TableB.TableARelationID AND TableB.Type = 2



回答6:


Try this

select TableA.RecordID, TableB.Text 
from TableA 
left join TableB on 
  TableA.RelationID = TableB.TableARelationID AND 
  TableB.Type = 2



回答7:


There are two straight forward versions of this query;

SELECT recordid, CASE WHEN type=2 THEN text ELSE NULL END text
FROM tableA
JOIN tableB
  ON tableA.relationid = tableB.tablearelationid;

This will show NULL for type <> 2 and not include lines that don't exist in both tables.

SELECT recordid, text
FROM tableA
LEFT JOIN tableB
  ON tableA.relationid = tableB.tablearelationid AND type=2;

This will do the same thing but include rows that only exist in TableA as NULL.

An SQLfiddle to test both and see the difference.



来源:https://stackoverflow.com/questions/20836566/sql-join-two-tables-with-specific-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!