3

ITEM我的table1 中有一个字段。如果ITEM包含%USED-%(USED-) 那么我想将其制作TIER1 Used Restaurant Equipment成相应的分组。

IF这是我目前的陈述,我怎样才能用or实现上述目标CASE

select 
    CT.TIER1, CT.TIER2
from
    trx.table1 MT
        LEFT JOIN
    trx.table2 CT1 ON MT.ITEM = CT1.ITEM
        LEFT JOIN
    trx.table3 CT ON CT1.WEB_NUM = CT.WEBNUMBER
GROUP BY CT.TIER1
LIMIT 0 , 15

电流输出:

TIER1                   TIER2
NULL                    NULL
Business Type           Ice Cream Equipment
Chinese Food Equipment  Ricers
Concession Equipment    Concession Accessories
Cooking Equipment       Fryers
Food Preparation        Food Preparation Supplies
Food Storage            Storage Racks
Food Warming Equipment  Hot Food Wells

我希望仍然有一些NULLTier1 和 Tier2,但我希望它也将 TIER1 显示为“使用过的餐厅设备”,类似于以下内容:

TIER1                      TIER2
NULL                       NULL
Used Restaurant Equipment  NULL
Business Type              Ice Cream Equipment
Chinese Food Equipment     Ricers
Concession Equipment       Concession 
4

2 回答 2

8

这应该是您正在寻找的:

   SELECT
       IF (MT.ITEM LIKE '%USED-%', 'Used Restaurant Equipment', CT.TIER1) AS TIER1,
       CT.TIER2
   FROM
       trx.table1 MT
           LEFT JOIN
       trx.table2 CT1 ON MT.ITEM = CT1.ITEM
           LEFT JOIN
       trx.table3 CT ON CT1.WEB_NUM = CT.WEBNUMBER
   GROUP BY
       IF (MT.ITEM LIKE '%USED-%', 'Used Restaurant Equipment', CT.TIER1)
   LIMIT 0 , 15
于 2012-12-11T17:13:44.477 回答
0

如果您不尝试重新定义 tier1,而是创建一个新列,该列从 tier1 或您的常量“使用的餐厅设备”中获取其值,则这很容易实现。然后,您可以根据用于创建新列的相同条件进行分组,因为 group by 子句可以是完整的表达式。

于 2012-12-11T17:07:36.910 回答