Pregunta

SQL Query - extract cell value for calculations as below

Info: Server version: 5.1.39
Php: 5.4
MySQL / phpMyAdmin
Server: Apache
Code is run via: Server SQL Query (copy & paste in the phpMyAdmin) or in MySQL Workbench or using a custom shopping cart manager.
Exports to: Excel (.csv then to .xlsx for sales reports)
Other: I do use a number of tables for referencing

I am trying to write a string (so far WHEN & IF) to do the following:

T5.coupon_code = XYZABC
AND T3.products_id=14  then the value is $5.00
as Ded_Promo

T5.coupon_code = XYZABC
AND (T3.products_id=Anything else)  then the value is $0.00
as Ded_Promo

(see table lines 1 & 2 for example).

code tried:

(IF(T5.coupon_code=XYZABC And T3.products_id=14,'5.00','0.00')) As Ded_promo2,

Result: Error Code: 1054. Unknown column 'XYZABC' in 'field list'

The coupon_code value is legit, when I run a query on the coupon_code the code that I am wanting to query shows as being used. (XYZABC is substituted for my real code).

Also tried:

CASE When (T5.coupon_code = XYZABC) then '5.00'
end As Ded_Promo,

Same error as above.

I then need to have the code further expanded to show:

If code & product match = $5.00 As Ded_Promo (as above)

But if code is present (say ID: ABC123DEF), then 'value' (as listed) As Disc_CoupVal

Ideally I need to make sure that the Promo discount doesn't get attributed to other products in the order, whilst the front end calculates it as 1 sale. My reports require that I keep discount coupons and special promotions connected to ONLY the product they are discounting. Commissions rely on this.

However I still need to see all other coupons listed and attribute them across an entire order of 1 or more products.

There will never be more than 1 coupon_code on any given order, the store doesn't allow it.

From above:

orderid | couponcode  | ProdID | Discount
21      | XYZABC      | 14     | 5.00
21      | XYZABC      | 12     | 0.00
36      | ABC123DEF   | 3      | 2.50
¿Fue útil?

Solución

(IF(T5.coupon_code=XYZABC And T3.products_id=14,'5.00','0.00')) As Ded_promo2

This is causing an error because you are using XYZABC as a column name. Use it as a string:

(IF(T5.coupon_code='XYZABC' And T3.products_id=14,'5.00','0.00')) As Ded_promo2

Edit:

In response to your comments, you can include these in the same column but it's a bit messier:

(IF(
     T5.coupon_code='XYZABC' And T3.products_id=14,
     '5.00',
      (IF(
           T5.coupon_code='AnyotherCoupon' And T3.products_id='AnyotherID',
      'CouponValue',
       '0.00'))
 )) As Ded_promo2,

So the above will show 5 if the code is XYZABC and product id is 14.

Otherwise if code is AnyotherCoupon and id is AnyotherID it will show CouponValue.

If neither of these are the case then it will show 0.00.

One thing to note that if there is a lot of these it would be better to store the promotion amount in the database (perhaps as a coupon table?). This keeps the SQL neater and allows you to more easily add and change coupons.

Otros consejos

(IF(T5.coupon_code='XYZABC' And T3.products_id=14,'5.00','0.00')) As Ded_promo2,

Try putting single quotes around the XYZABC. This makes the term a literal character string, instead of a named data item in SQL.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top