mysql - 使用子查询作为字段列

标签 mysql sql subquery

我有一个以子查询作为字段列的查询,但我想在其他地方使用这个字段列。

SELECT c.country_code                                                    AS 
       country_code, 
       c.dial_code                                                       AS 
       dial_code, 
       (SELECT r.destination 
        FROM   region r 
        WHERE  r.country_code = c.country_code 
               AND r.dial_code = c.dial_code)                            AS 
       destination, 
       c.start_time, 
       c.duration, 
       c.call_type, 
       c.customer_prefix                                                 AS 
       customer_prefix, 
       c.vendor_prefix                                                   AS 
       vendor_prefix, 
       (SELECT Round(r.rate, 3) 
        FROM   rate r 
               INNER JOIN region re 
                       ON r.region_id = re.id 
               INNER JOIN account_prefix ap 
                       ON r.account_prefix_id = ap.id 
        WHERE  re.country_code = c.country_code 
               AND re.dial_code = c.dial_code 
               AND ap.prefix = c.customer_prefix 
               AND ap.prefix_type = 0)                                   AS 
       **customer_rate**, 
       (SELECT Round(r.rate, 3) 
        FROM   rate r 
               INNER JOIN region re 
                       ON r.region_id = re.id 
               INNER JOIN account_prefix ap 
                       ON r.account_prefix_id = ap.id 
        WHERE  re.country_code = c.country_code 
               AND re.dial_code = c.dial_code 
               AND ap.prefix = c.vendor_prefix 
               AND ap.prefix_type = 1)                                   AS 
       **vendor_rate**, 
       (SELECT Round(r.rate, 3) 
        FROM   rate r 
               INNER JOIN region re 
                       ON r.region_id = re.id 
               INNER JOIN account_prefix ap 
                       ON r.account_prefix_id = ap.id 
        WHERE  re.country_code = c.country_code 
               AND re.dial_code = c.dial_code 
               AND ap.prefix = c.customer_prefix 
               AND ap.prefix_type = 0) - (SELECT Round(r.rate, 3) 
                                          FROM   rate r 
                                                 INNER JOIN region re 
                                                         ON r.region_id = re.id 
                                                 INNER JOIN account_prefix ap 
                                                         ON r.account_prefix_id 
                                                            = ap.id 
                                          WHERE 
       re.country_code = c.country_code 
       AND re.dial_code = c.dial_code 
       AND ap.prefix = c.vendor_prefix 
       AND ap.prefix_type = 1) AS **unit_profit**, 
       unit_profit * duration 
FROM   cdr c 
LIMIT  100; 

如您所见,我想使用 unit_profit、customer_rate 和 vendor_rate。如何实现呢?

编辑:

有没有在 View 中显示连接的教程?

最佳答案

您需要做的是获取所有在 flied 中完成的子查询,并创建与 CDR 表的连接。

这将大大提高查询性能和响应时间。您现在正在为 CDR 中的记录执行 3 个查询。如果这个表 (CDR) 只有几条记录就​​没问题,但如果不是这样,这可能会消耗大量处理器、内存和磁盘 I/O。

执行“连接”并以相同格式显示信息的技巧是连接 3 次相同的子查询,但使用不同的别名。

select  c.country_code, customer_rate_table.customer_rate 
from CDR c
left outer join on ( SELECT Round(r.rate, 3) customer_rate , re.country_code, 
                       re.dial_code, re.dial_code, ap.prefix  
               FROM   rate r 
               INNER JOIN region re 
                       ON r.region_id = re.id 
               INNER JOIN account_prefix ap 
                       ON r.account_prefix_id = ap.id 
               WHERE ap.prefix_type = 1
        
) customer_rate_table
ON  customer_rate.country_code = c.country_code 
AND customer_rate.dial_code = c.dial_code 
AND customer_rate.prefix = c. customer_prefix 
left outer join on ( {Same as above but with the right fields} ) vendor_rate_table
ON  vendor_rate_table.country_code = c.country_code 
AND vendor_rate_table.dial_code = c.dial_code 
AND vendor_rate_table.prefix = c.vendor_prefix 

然后下一个表...

这段代码并不完整,但我认为可以解释您需要做什么。

谢谢!

@里奥

关于mysql - 使用子查询作为字段列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15285413/

相关文章:

sql - Snowflake 使用 flatten 进行数组展平返回未知函数

sql - 参数化 SQL 中的子查询

sql - 在选择中返回一个 json 对象 - 导致速度变慢

mysql - 获取正确年份的sql查询

php - 组合来自不同表、具有不同字段名称的 2 个查询

mysql - SQL语言条件多表收集谁信息

sql - 如何调试 "sql subquery returns more than 1 row"错误

php - 在php中从mysql获取两个字段之间的唯一记录

MySql:在两个表上使用内部联接进行更新挂起

php - 使用 HABTM 查找未显示 CakePHP 中引用表结果的查询