选择一个数字范围

时间:2012-11-26 08:09:41

标签: mysql sql

查询:

SELECT nummer
   FROM tabell_med_nummer
  WHERE SUBSTR(nummer, -1) = 0

此查询选择表tabell_med_nummer中以“0”结尾的数字。例如,它将选择1000,1010,1020,但不选择1001,1002等。

我需要根据这个得到一个范围。例如1000-1100或1010-1023。范围的大小是可变的。它的结尾并不重要,但它必须选择一个以“0”结尾的数字,如1010。

棘手的部分是表中不存在每个数字。因此,如果表格中不存在1004,那么我需要100的范围,它不能从1000开始。

有谁知道如何解决这个问题?

根据要求:

    +----+-------------+-------+-------+-------------------------------+---------------+---------+------+------+---------------------------------+
| id | select_type | table | type  | possible_keys                 | key           | key_len | ref  | rows | Extra                           |
+----+-------------+-------+-------+-------------------------------+---------------+---------+------+------+---------------------------------+
|  1 | SIMPLE      | f     | ALL   | NULL                          | NULL          | NULL    | NULL |   19 | Using temporary; Using filesort |
|  1 | SIMPLE      | t2    | index | telefonnummer,telefonnummer_2 | telefonnummer | 5       | NULL | 1001 | Using index; Using join buffer  |
|  1 | SIMPLE      | p     | ALL   | NULL                          | NULL          | NULL    | NULL | 4568 | Using where; Using join buffer  |
|  1 | SIMPLE      | t3    | index | telefonnummer,telefonnummer_2 | telefonnummer | 5       | NULL | 1001 | Using index; Using join buffer  |
|  1 | SIMPLE      | t1    | ALL   | telefonnummer,telefonnummer_2 | NULL          | NULL    | NULL | 1001 | Using where; Using join buffer  |
+----+-------------+-------+-------+-------------------------------+---------------+---------+------+------+---------------------------------+
5 rows in set (0.00 sec)

我更改了一些表和字段名称,并添加了一些连接和法规:

SELECT t1.telefonnummer start, t2.telefonnummer end, t1.kundenummer kundenummer, t1.fornavn fornavn, t1.etternavn etternavn, t1.bedriftsnavn bedriftsnavn, t1.organisasjonsnummer organisasjonsnummer, t1.partnerID partnerID

FROM TELEFONNUMMERTILDELING t1

JOIN TELEFONNUMMERTILDELING t2
ON t1.telefonnummer % 10 = 0
AND t1.telefonnummer <= t2.telefonnummer

JOIN TELEFONNUMMERTILDELING t3
ON t3.telefonnummer BETWEEN t1.telefonnummer AND t2.telefonnummer

JOIN TELEFONNUMMERTILDELING_POSTNUMMER p
ON p.postnummer = 4085

JOIN TELEFONNUMMERTILDELING_FYLKE f
ON f.ID = p.fylkeID GROUP BY start, end

HAVING end - start + 1 = COUNT(*)
AND end - start + 1 = 50
AND (kundenummer IS NULL OR kundenummer = '')
AND (fornavn IS NULL OR fornavn = '')
AND (etternavn IS NULL OR etternavn = '')
AND (bedriftsnavn IS NULL OR bedriftsnavn = '')
AND (organisasjonsnummer IS NULL OR organisasjonsnummer = '')
AND partnerID = 1001 

2 个答案:

答案 0 :(得分:2)

-- get the start and end points
SELECT   t1.nummer AS start, t2.nummer AS end

-- pair every possible start number with every potential end number
FROM     tabell_med_nummer t1
    JOIN tabell_med_nummer t2
      ON t1.nummer % 10 = 0 AND t1.nummer <= t2.nummer

-- obtain every number in between
    JOIN tabell_med_nummer t3
      ON t3.nummer BETWEEN t1.nummer AND t2.nummer

-- group into potential ranges
GROUP BY start, end

-- now limit only to contiguous ranges
  HAVING end - start + 1 = COUNT(*)

-- and those that contain the desired number of records
     AND end - start + 1 = ?

如果nummer不能保证唯一,例如如果使用UNIQUE约束,则需要将COUNT(*)替换为性能较低的COUNT(DISTINCT t3.nummer),否则将t3替换为(SELECT DISTINCT nummer FROM tabell_med_nummer)

答案 1 :(得分:1)

WHERE子句中添加其他条件。

 SELECT nummer
   FROM tabell_med_nummer
  WHERE SUBSTR(nummer, -1) = 0 AND
        nummer BETWEEN 1000 AND 1100