我有一个将子查询作为字段列的查询,但我想在其他地方使用这个字段列。
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。如何实现呢?
编辑:
任何显示加入视图的教程?