取得相同位数的号码

时间:2019-07-05 20:16:13

标签: mysql sql

MySQL数据库:

library(zoo)
serie1 <- zoo(rnorm(365), seq(as.Date('2001-01-01'),as.Date('2001-12-31'),by = 1))
serie2 <- zoo(rnorm(365), seq(as.Date('2002-01-01'),as.Date('2002-12-31'),by = 1))
serie3 <- zoo(rnorm(365), seq(as.Date('2003-01-01'),as.Date('2003-12-31'),by = 1))

plot(serie1, col = 2, ylim = c(-4,4), ylab = "", xlab = "Month")
par(new = T)
plot(serie2, col = 3, ylim = c(-4,4), ylab = "", xlab = "Month")
par(new = T)
plot(serie3, col = 4, ylim = c(-4,4), ylab = "", xlab = "Month")
legend("topleft", legend = paste("year",2001:2003), col = 2:4, lty = 1)

使用

可以轻松获取相同的序列,例如: ABCDE-ABCDE
+-----------+
| Number    | {bigint(10)}
+-----------+
| 9599595995|
+-----------+
| 8438084380|
+-----------+
| 8959329599|
+-----------+
| 7778278236|
+-----------+

结果:-

  

9599595995和8438084380

要提取相同的3位数字的查询是什么:-xxabcabcxx或xxabcxxabc或abcxxxabcx或abcabcxxxx或xxxxabcabc

结果:-

  

9599595995,8438084380,8959329599和7778278236

2 个答案:

答案 0 :(得分:1)

先检查10-3 * 2 + 1 = 5个位置

SELECT * FROM numbers
WHERE POSITION(SUBSTRING(Number,1,3) IN SUBSTRING(Number,4,10)) > 0
    OR POSITION(SUBSTRING(Number,2,3) IN SUBSTRING(Number,5,10)) > 0
    OR POSITION(SUBSTRING(Number,3,3) IN SUBSTRING(Number,6,10)) > 0
    OR POSITION(SUBSTRING(Number,4,3) IN SUBSTRING(Number,7,10)) > 0
    OR SUBSTRING(Number,5,3) = SUBSTRING(Number,8,10)

答案 1 :(得分:1)

使用UNION ALL构造一系列从1到5的数字,以便获取每个数字的所有可能的3位数子字符串:

select distinct n.number
from (
  select 1 start union all select 2  union all select 3 union all 
  select 4 union all select 5 
) s cross join numbers n
where n.number like concat(
  '%', substring(n.number, s.start, 3), 
  '%', substring(n.number, s.start, 3), '%'
)

请参见demo(我添加了更多行)。
使用:

insert into numbers (number) values
('1234567890'),
('1234567123'), 
('4567123123'),                                      
('1231245675'),                                     
('9599595995'),
('8438084380'),
('8959329599'),
('7778278236'); 

结果:

| number     |
| ---------- |
| 1234567123 |
| 4567123123 |
| 9599595995 |
| 8438084380 |
| 8959329599 |
| 7778278236 |