SQL not recognizing column alias in where clause

后端 未结 3 1976
执念已碎
执念已碎 2020-12-03 14:26

I am only a beginner in SQL, but I\'ve come across this annoying error. SQL is having an issue with the WHERE clause of this script:

SELECT
  ITEM_ID, ITEM_P         


        
3条回答
  •  醉酒成梦
    2020-12-03 15:05

    You cannot use the column name which is used as alias one in the query

    Reason:

    The query will first checks for runtime at that time the column name "item_total" is not found in the table "ORDER_ITEMS" because it was give as alias which is not stored in anywhere and you are assigning that column in desired output only

    Alternate:

    If you want to use that type go with sub queries it's performance is not good but it is one of the alternate way

    SELECT * FROM
     (SELECT
      ITEM_ID, ITEM_PRICE, DISCOUNT_AMOUNT, QUANTITY, 
      (ITEM_PRICE*QUANTITY) AS price_total, 
      (DISCOUNT_AMOUNT*QUANTITY) AS discount_total, 
      ((ITEM_PRICE-DISCOUNT_AMOUNT)*QUANTITY) AS item_total
     FROM ORDER_ITEMS) as  tbl
    WHERE tbl.item_total > 500
    ORDER BY tbl.item_total;
    

提交回复
热议问题