How select for not used codes in this sample

后端 未结 2 1243
旧巷少年郎
旧巷少年郎 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:43

    IMHO you should maintain the list of valid codes in a look up table in the database and have the used codes as a foreign key field in the other table (MyTable in your post).

    Assuming you have created a table to save codes with name LookUpTable that has a code id and code. In the other table, you have code id as foriegn key. Then, you can use following query:

    SELECT  LOOKUPTABLE.Code 
    FROM    LOOKUPTABLE LEFT JOIN FOREIGNKEYTABLE ON LOOKUPTABLE.ID = FOREIGNKEYTABLE.CodeID 
    WHERE FOREIGNKEYTABLE.USERID IS NULL
    

提交回复
热议问题