SQL查询-引用内部查询

时间:2018-12-04 18:44:08

标签: mysql

我不是经验丰富的SQL编写者,但是可以执行泛型选择和联接,但是我有一个更复杂的SQL,我试图将其写在需要获取内部结果集并基于该联接进行联接的地方它。

我的子查询或内部查询是这样的:

 ( 
 SELECT    a.item_code, 
                     Concat('$', Format(Sum(a.standard),2, 'tan_in')) AS `standard`, 
                     concat('$', format(sum(a.si),2, 'tan_in'))       AS `si`, 
                     concat('$', format(sum(a.tessco),2, 'tan_in'))   AS `tessco price list` 
 FROM      ( 
                           SELECT DISTINCT t.item_code, 
                                           t.standard, 
                                           t.si, 
                                           t.tessco 
                           FROM            `tabitem price` AS t1 
                           JOIN 
                                           ( 
                                                           SELECT DISTINCT p.item_code,IF(p.price_list = "Standard Selling", p.price_list_rate, 0)  as standard,
                                                                           IF(p.price_list = "SI",p.price_list_rate,0)                  AS si,
                                                                           IF(p.price_list = "Tessco Price List", p.price_list_rate, 0) AS tessco
                                                           FROM            `tabitem price` p 
                                                           INNER JOIN      `tabitem price` r 
                                                           ON              r.item_code = p.item_code
                                                           ORDER BY        item_code) t 
                           ON              t1.item_code = t.item_code) a 
 LEFT JOIN `tabitem` tabi 
 ON        tabi.NAME = a.item_code 
 GROUP BY  a.item_code) AS final

除此之外,我还有:

SELECT
tabCustomer.customer_name AS 'Customer::150', 
CONCAT(FORMAT(`tabPricing Rule`.discount_percentage,2),'%%') AS 'Discount %%', 
`tabPricing Rule`.item_group AS 'Discount Group',
`tabPricing Rule`.title AS 'Discount Title',
final.item_code AS 'Item::125', 
tabItem.description AS 'Description::250',
`tabItem Price`.price_list AS 'Price List::125',
final.Standard AS 'List Price',
final.SI AS 'SI Price',
CONCAT('$', FORMAT(`tabItem Price`.price_list_rate,2)) AS 'Customer Price::125',
IF(`tabPricing Rule`.item_group = "Top level", CONCAT('$', FORMAT(`tabItem Price`.price_list_rate * (1-(`tabPricing Rule`.discount_percentage/100)),2)), if(`tabPricing Rule`.item_group = tabItem.item_group, CONCAT('$', FORMAT(`tabItem Price`.price_list_rate * (1-(`tabPricing Rule`.discount_percentage/100)),2)), CONCAT('$', FORMAT(`tabItem Price`.price_list_rate,2)))) AS 'Discounted Price',
tabItem.item_group AS 'Category::100',
tabItem._user_tags AS 'User Tag::100'
FROM final  

/ *内部查询在这里* /

WHERE tabitem.disabled = '0' 
AND  tabitem.item_group != 'Third Party Components' 
AND  tabitem.item_group != 'Engineering Services' 
AND  tabitem.item_group != 'Services' 
ORDER BY tabitem.item_group, 
         tabitem._user_tags, 
        `tabitem price`.price_list_rate;

运行此命令时,它说“最终”表不存在。

我的问题是如何引用内部结果集?

谢谢。

此致

JR

1 个答案:

答案 0 :(得分:0)

不确定这是问题还是成绩单上的错误。

但是现在你有

FROM final  
/* inner query goes here */

WHERE tabitem.disabled = '0' 

什么时候应该拥有

FROM ( * inner query goes here */ ) as final  

WHERE tabitem.disabled = '0' 

我也看不到外部查询中包含tabitem的位置。