复杂的多表mysql查询

时间:2018-01-17 05:26:56

标签: php mysql

我正在寻找一些有关mysql查询的帮助,以便从我的国际通话计费系统的几个不同表格中返回以下数据

在数据库中,我有一个表格,其中包含每个通话记录,结算费率和我希望在发票上显示的最后一组。

我想要做的是遍历表tbl_international_rates,并且对于tbl_international_records中的每一行总和CDR_ChargedDurationMinutes WHERE CDR_BillerCode = international_rate_prefix然后* international_rate_price返回总计收费分钟数和前缀总数或类似下面的内容。< / p>

|--------------------------|--------------------------|--------------------------|
|        Prefix            |      total minutes       | total amount         |
|--------------------------|--------------------------|--------------------------|
|         93               |          14.3            |         $137.28          |
|--------------------------|--------------------------|--------------------------|
|         937              |          31.2            |         $418.08          |
|--------------------------|--------------------------|--------------------------|

一旦完成,我想将它们组合在一起,由父组返回mysql的内容,如下表所示。

|--------------------------|--------------------------|--------------------------|
|  CountryDescription      |   CountryTotalMinutes    |      CountryTotalCosts   |
|--------------------------|--------------------------|--------------------------|
|        Afghanistan       |         45.5             |           555.36         |
|--------------------------|--------------------------|--------------------------|

这可能吗?

这是数据库中表格的结构

tbl_international_groups

|--------------------------|--------------------------------|
| international_group_idno | international_group_description|
|--------------------------|--------------------------------|
|            1             |         Afghanistan            |
|--------------------------|--------------------------------|
|            2             |         USA                    |
|--------------------------|--------------------------------|

tbl_international_rates

|--------------------------|--------------------------|--------------------------|--------------------------|
| international_rate_idno  | international_rate_prefix| international_rate_group | international_rate_price |
|--------------------------|--------------------------|--------------------------|--------------------------|
|            1             |         93               |          1               |          9.60            |
|--------------------------|--------------------------|--------------------------|--------------------------|
|            1             |         937              |          1               |          13.40           |
|--------------------------|--------------------------|--------------------------|--------------------------|

tbl_international_records

|--------------------------|--------------------------|--------------------------|
|          CDR_ID          |CDR_ChargedDurationMinutes|      CDR_BillerCode      |
|--------------------------|--------------------------|--------------------------|
|            1             |         4.7              |           93             |
|--------------------------|--------------------------|--------------------------|
|            2             |         5.6              |           97             |
|--------------------------|--------------------------|--------------------------|
|            3             |         9.6              |           93             |
|--------------------------|--------------------------|--------------------------|
|            4             |         25.6             |           97             |
|--------------------------|--------------------------|--------------------------|

**** ****** EDIT

使用下面的查询,我可以像下面那样返回表格,但是如何按照tbl_international_groups中的父母对它们进行分组

|--------------------------|--------------------------|--------------------------|
|        Prefix            |      total minutes       | total amount         |
|--------------------------|--------------------------|--------------------------|
|         93               |          14.3            |         $137.28          |
|--------------------------|--------------------------|--------------------------|
|         937              |          31.2            |         $418.08          |
|--------------------------|--------------------------|--------------------------|

查询

select rates.international_rate_prefix as Prefix
 , sum( records.CDR_ChargedDurationMinutes ) as total_minutes
 , sum( records.CDR_ChargedDurationMinutes ) * 
rates.international_rate_retail as total_amount
from tbl_international_rates as rates
join tbl_international_records as records
  on rates.international_rate_prefix = records.CDR_BillerCode
group by rates.international_rate_prefix

1 个答案:

答案 0 :(得分:-1)

修正了它。这是我的解决方案:http://sqlfiddle.com/#!9/8eb3b1/23

select international_group_description as CountryDescription,
 sum(total_minutes) as CountryTotalMinutes,
 sum(total_amount) as CountryTotalCosts
from (
  select rates.international_rate_group, 
  rates.international_rate_prefix as Prefix
  , round(sum( records.CDR_ChargedDurationMinutes ), 2) as total_minutes
  , round(sum( records.CDR_ChargedDurationMinutes ) * 
   rates.international_rate_price, 2) as total_amount
  from tbl_international_rates as rates
  join tbl_international_records as records
    on rates.international_rate_prefix = records.CDR_BillerCode
  join tbl_international_groups groups
    on rates.international_rate_group = 
  groups.international_group_idno
  group by rates.international_rate_prefix
 ) as subselect
join tbl_international_groups groups
on groups.international_group_idno = subselect.international_rate_group