SQL: Select Keys that doesn't exist in one table

前端 未结 6 707
刺人心
刺人心 2021-01-11 11:24

I got a table with a normal setup of auto inc. ids. Some of the rows have been deleted so the ID list could look something like this:

(1, 2, 3, 5, 8,

6条回答
  •  逝去的感伤
    2021-01-11 11:55

    The problem is that T1 could have a million rows or ten million rows, and that number could change, so you don't know how many rows your comparison table, T2, the one that has no gaps, should have, for doing a WHERE NOT EXISTS or a LEFT JOIN testing for NULL.

    But the question is, why do you care if there are missing values? I submit that, when an application is properly architected, it should not matter if there are gaps in an autoincrementing key sequence. Even an application where gaps do matter, such as a check-register, should not be using an autoincrenting primary key as a synonym for the check number.

    Care to elaborate on your application requirement?

提交回复
热议问题