sql查询-引用内部查询

92dk7w1h  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(370)

我不是一个经验丰富的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;

当我运行这个时,它说“final”表不存在。
我的问题是如何引用内部结果集?
谢谢您。
当做,
年少者

x6yk4ghg

x6yk4ghg1#

不确定这是问题还是笔录上的错误。
但现在你有

FROM final  
/* inner query goes here */

WHERE tabitem.disabled = '0'

你应该什么时候

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

WHERE tabitem.disabled = '0'

我也不知道你包括在哪里 tabitem 在外部查询中。

相关问题