T-SQL: Comparing Two Tables - Records that don't exist in second table

前端 未结 7 1368
小鲜肉
小鲜肉 2021-01-02 05:05

If UNION ALL is an addition in T-SQL. What is the equivalent of subtraction?

For example, if I have a table PEOPLE and a table

7条回答
  •  无人及你
    2021-01-02 05:44

    Unfortunately there is a problem in your design. instead of having two table PEOPLE and CONTRACTOR. You should have a table PEOPLE and another Table TYPE (if some people can have several role another table maybe needed). In your PEOPLE table you make a referece to the TYPE table.

    then you requests become

    SELECT * from PEOPLE, TYPE
    WHERE PEOPLE.type_id = TYPE.id 
    AND TYPE.name = 'CONTRACTOR'
    
    SELECT * from PEOPLE, TYPE
    WHERE PEOPLE.type_id = TYPE.id 
    AND TYPE.name = 'EMPLOYEE'
    

    (untested)

提交回复
热议问题