How to conditionally get price*quantity using CASE
P粉506963842
P粉506963842 2024-04-03 20:45:07
0
1
525

If I have sale_price, I will use sale_price * quantity to represent the total price, If you don't have a sale_price, you need to express the total price as Price_origin * quantity.

I tried using CASE as below but got this error.

Error 1064 (42000): There is an error in your SQL syntax; check the manual for your MySQL server version for ') )AS products FROM users u JOIN cart_order c_o ON c_o.user_id = Correct syntax to use around u'

What did i do wrong? Is there any other method? I need help.

const readUserCart = async userId => {
  const userCart = await myDataSource.query(
    `SELECT u.id AS userId, u.name AS userName,
    JSON_ARRAYAGG(
        JSON_OBJECT(
          'productName', p.name, 
          'quantity', c_o.quantity,
          'priceOrigin', REPLACE(p.price_origin, '.', ','),
          'salePrice', REPLACE(p.sale_price, '.', ','),
          'productThumbnail', t_i.default_img,
          'status', c_o.status,
          'price', CASE 
                  WHEN p.sale_price = null 
                  THEN REPLACE(p.price_origin * quantity, '.', ',') 
                  ELSE REPLACE(p.sale_price * quantity, '.', ',')
        )
      )AS products
    FROM users u
    JOIN cart_order c_o ON c_o.user_id = u.id
    JOIN products p ON p.id = c_o.product_id
    WHERE u.id = ?
    GROUP BY u.id`,
    [userId]
  );
  return userCart;
};

P粉506963842
P粉506963842

reply all(1)
P粉138711794

Try to compare "sale_price is null" with "sale_price = null". Maybe it helps you.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template