How select for not used codes in this sample

后端 未结 2 1235
旧巷少年郎
旧巷少年郎 2021-01-28 13:39

I have a int column in my table in a SQL database. I keep some of codes in this table. For sample range of my codes is : (1, 9).
I need to not use

2条回答
  •  醉梦人生
    2021-01-28 14:19

    Generate a list of numbers for your range (here I use VALUES clause), then semi-join to the "used" list

    SELECT
        *
    FROM
       (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9)) X (Num)
    WHERE
       NOT EXISTS (SELECT * FROM MyTable M WHERE M.Code = X.Num)
    

    Edit:

    You can replace the VALUES clause with any number table generation code. Examples:

    • Optimizing Numbers Table Creation on SQL Server?
    • What is the best way to create and populate a numbers table?

提交回复
热议问题