我正在尝试比较两个电话提供商之间的通话费率。我有两张表,如下:
CREATE TABLE 18185_rates (
calldate DATE,
calltime TIME,
calledno VARCHAR(20),
duration INTEGER(8),
callcost FLOAT(5 , 3 )
);
CREATE TABLE int_rates (
dialcode VARCHAR(20),
description VARCHAR(20),
callcost FLOAT(5 , 3 )
);
18185_rates 包含来自电话系统的呼叫数据记录,一些示例值如下:
calldate,calltime,calledno,duration,callcost
2013-07-30,11:21:38,35342245738,10,0.050
2013-07-30,16:19:25,353872565822,37,0.130
2013-08-02,08:31:12,65975636187,1344,0.270
2013-08-05,11:03:53,919311195965,2356,1.640
表 int_rates 包含来自其他提供商的呼叫的资费数据,格式如下:
dialcode,description,callcost
1,USA,0.012
1204,Canada,0.008
1204131,Canada,0.018
1226,Canada,0.008
1226131,Canada,0.018
1242,Bahamas,0.137
1242357,Bahamas Mobile,0.251
1242359,Bahamas Mobile,0.251
我正在尝试进行比较,以便我可以看到 18185_rates 中的调用与其他提供商的成本。我不知道如何根据 int_rates 中的可变长度拨号代码连接这两个表。
在下面的@Gordon Linoff 的帮助下,我想出了以下代码:
SELECT
r.*,
(SELECT permin
FROM int_rates ir1
WHERE r.calledno LIKE CONCAT(ir1.dialcode, '%')
ORDER BY dialcode DESC
LIMIT 1) AS newcostpermin
FROM
18185_rates r;