sql 判斷連續數字

設表名爲TABLENAME,字段名爲FIELD1則:
SELECT
	FIELDN = FIELD1 
FROM
	TABLENAME 
WHERE
	NOT EXISTS (
SELECT
	FIELD1 
FROM
	TABLENAME A 
WHERE
	A.FIELD1 = tablename.field1 + 1 
	) UNION
SELECT
	FIELDN = FIELD1 
FROM
	TABLENAME 
WHERE
	NOT EXISTS (
SELECT
	FIELD1 
FROM
	TABLENAME A 
WHERE
	A.FIELD1 + 1 = tablename.field1 
	) 
ORDER BY
	FIELDN
相關文章
相關標籤/搜索