1. 程式人生 > >sql 判斷連續數字

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