sql - postgres正则表达式国际电话代码匹配

标签 sql regex postgresql

当我在下面插入代码时

select (select i.billsec from isp_cdr i) * 
       (select l.user_rate from lcr l ) as nibble_total_billed
where i.destination_number !~'1548749' 
and i.destination_number ~'^(?:[0-7] ?){6,14}[0-9]$' = l.digits;

我收到这个错误

ERROR: missing FROM-clause entry for table "i"
LINE 2: where i.destination_number !~'1548749'

isp_cdr 表:

destination_number
nibble_total_billed
caller_id
billsec

lcr表:

user_rate
digits 

数字是电话代码 destination_number 是用户拨出的电话号码

此查询的目的是使用国家代码(数字)计算账单支票,然后使用 user_rate 将 billsec 更新乘以 nibble_total_billed

样本数据

isp_cdr 表

destination_number           billsec
60161234587                    50 
60134812315                   127 
60147123512                    98 

lcr表

digits             user_rate
601                   0.15 
4672                  1.6
61891010              1.69

最佳答案

我不清楚你想在这里实现什么。

立即回答您为什么会收到错误的问题。

你的陈述基本上是这样的:

select (..) * (...) as nibble_total_billed
-- you are missing a FROM after the SELECT
where i.destination_number !~'1548749' 
  and i.destination_number ~'^(?:[0-7] ?){6,14}[0-9]$' = l.digits;

似乎正在寻找两个表之间的联接:

select i.billsec * l.user_rate as nibble_total_billed
from isp_cdr i
  join l.user_rate from l on i.destination_number = l.digits
where i.destination_number !~ '1548749' 
  and i.destination_number ~'^(?:[0-7] ?){6,14}[0-9]$'

鉴于(无效)条件 i.destination_number ~'^(?:[0-7] ?){6,14}[0-9]$' = l.digits 似乎您想在将 destination_numberdigits 列进行比较之前以某种方式“清理”该 destination_number 但由于您没有显示任何示例数据,因此无法回答这部分问题。

关于sql - postgres正则表达式国际电话代码匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40128216/

相关文章:

python - 为什么 postgresql 不立即开始返回行?

python - 生成SQL字符串的设计模式

regex - 从R中的单个字符串中提取所有数字

arrays - pg 删除数组中的公共(public)部分

C# 将正则表达式匹配到 HHMMSS

正则表达式模式可选字符

postgresql - Sequelize 查询 child 的位置

php - 我们如何根据 php 和 mysql 中关键字的出现对搜索结果进行排序

sql - 如何在 Oracle SQL 中使用回滚/提交

mysql - 根据 2 个条件从多个 MySQL 表中获取最新记录